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.

Title: Lambda in list comprehension
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: dsblank, rhettinger
Priority: normal Keywords:

Created on 2004-10-29 00:59 by dsblank, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg22930 - (view) Author: Doug Blank (dsblank) Date: 2004-10-29 00:59
I know that this has been broken for a long time, but I
thought that it would be addressed by 2.4. I couldn't
find it in the bug database. Here's the problem:

$ python2.4
Python 2.4b1 (#1, Oct 25 2004, 01:44:03)
[GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> x = [lambda:a for a in range(5)]
>>> x[0]()
>>> x[1]()

The workaround is the old bind-the-arg trick:

>>> x = [lambda a=a:a for a in range(5)]
>>> x[0]()
>>> x[1]()
msg22931 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-10-29 01:50
Logged In: YES 

Sorry, this is not a bug.

Someone on comp.lang.python or python-tutor would be happy
to explain it.
Date User Action Args
2022-04-11 14:56:07adminsetgithub: 41097
2004-10-29 00:59:42dsblankcreate