[issue45469] lambda issue in for-loop

2021-10-14 Thread Steven D'Aprano
Steven D'Aprano added the comment: See also the FAQs: https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result -- nosy: +steven.daprano ___ Python tracker

[issue45469] lambda issue in for-loop

2021-10-14 Thread Christian Heimes
Christian Heimes added the comment: You are running in a typical scope issue. The local and global scope of a lambda work differently than you expect. You can work around the issue by making fun a local variable: for name, fun in namefun: name2fun[name] = lambda x, fun=fun: fun(x)

[issue45469] lambda issue in for-loop

2021-10-14 Thread Vincent Liang
New submission from Vincent Liang : Strange behavior is found when lambda is used inside for-loop. code:(same as attached file) # begin of CODE def aa(x): print("aa") def bb(x): print("bb") namefun = [ ("a", aa), ("b", bb), ] name2fun = {} for name, fun in namefun: