This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vincent7f
Recipients vincent7f
Date 2021-10-14.12:20:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1634214021.97.0.77826806934.issue45469@roundup.psfhosted.org>
In-reply-to
Content
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:
    name2fun[name] = lambda x: fun(x)
# issue happened when calling lambda
name2fun["a"](1)
name2fun["b"](1)
# end of CODE

output:
bb
bb

expected output:
aa
bb
History
Date User Action Args
2021-10-14 12:20:21vincent7fsetrecipients: + vincent7f
2021-10-14 12:20:21vincent7fsetmessageid: <1634214021.97.0.77826806934.issue45469@roundup.psfhosted.org>
2021-10-14 12:20:21vincent7flinkissue45469 messages
2021-10-14 12:20:21vincent7fcreate