List comp bug?
Terry Reedy
tjreedy at udel.edu
Thu Apr 13 17:18:25 EDT 2006
<tasjaevan at gmail.com> wrote in message
news:1144923444.813998.61650 at v46g2000cwv.googlegroups.com...
> [f() for f in [lambda: t for t in ((1, 2), (3, 4))]]
> is giving me
> [(3, 4), (3, 4)]
>
> The equivalent using a generator expression:
List comps were originally designed to be equivalent to for loops with
nested for loops and if blocks. Generator expressions are know to be
slightly different and *not* exactly equivalent due to the new local
namespace.
> [f() for f in (lambda: t for t in ((1, 2), (3, 4)))]
> is giving me
> [(1, 2), (3, 4)]
Since, when there is a difference, as above, the genex behavior is more
often the right or expected behavior (as above), Guido is considering
making the diference go away in 3.0 by making [stuff] == list((stuff)) (ie,
listcomp == list(genex)).
Terry Jan Reedy
More information about the Python-list
mailing list