[New-bugs-announce] [issue45469] lambda issue in for-loop

Vincent Liang report at bugs.python.org
Thu Oct 14 08:20:21 EDT 2021


New submission from Vincent Liang <shenheng80 at qq.com>:

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

----------
components: Interpreter Core
files: test8.py
messages: 403899
nosy: vincent7f
priority: normal
severity: normal
status: open
title: lambda issue in for-loop
type: behavior
versions: Python 3.10, Python 3.9
Added file: https://bugs.python.org/file50357/test8.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45469>
_______________________________________


More information about the New-bugs-announce mailing list