[ python-Bugs-1056483 ] Lambda in list comprehension

SourceForge.net noreply at sourceforge.net
Fri Oct 29 03:50:04 CEST 2004


Bugs item #1056483, was opened at 2004-10-28 19:59
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1056483&group_id=5470

Category: Parser/Compiler
Group: Python 2.4
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Doug Blank (dsblank)
Assigned to: Nobody/Anonymous (nobody)
Summary: Lambda in list comprehension

Initial Comment:
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]()
4
>>> x[1]()
4

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

>>> x = [lambda a=a:a for a in range(5)]
>>> x[0]()
0
>>> x[1]()
1


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2004-10-28 20:50

Message:
Logged In: YES 
user_id=80475

Sorry, this is not a bug.

Someone on comp.lang.python or python-tutor would be happy
to explain it.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1056483&group_id=5470


More information about the Python-bugs-list mailing list