List comp bug?

Paul Rubin http
Thu Apr 13 06:37:21 EDT 2006


tasjaevan at gmail.com writes:
> I seem to have stumbled across a problem with list comprehensions (or
> perhaps it's a misunderstanding on my part)
> 
>    [f() for f in [lambda: t for t in ((1, 2), (3, 4))]]

List comprehensions are syntactic sugar for "for" loops.  The
thing to notice is that in "lambda: t", t is unbound.  It's not
the same as the loop index inside the list comp.  Try:

   [f() for f in [lambda t=t: t for t in ((1, 2), (3, 4))]]



More information about the Python-list mailing list