What's do list comprehensions do that generator expressions don't?

Bill Mill bill.mill at gmail.com
Mon Apr 25 16:48:46 EDT 2005


On 25 Apr 2005 23:33:48 +0300, Ville Vainio <ville at spammers.com> wrote:
> >>>>> "Jeremy" == Jeremy Bowers <jerf at jerf.org> writes:
> 
>     Jeremy> On Sun, 24 Apr 2005 22:59:12 -0700, Robert Kern wrote:
>     >> Never. If you really need a list
>     >>
>     >> list(x*x for x in xrange(10))
>     >>
>     >> Sadly, we can't remove list comprehensions until 3.0.
> 
>     Jeremy> Why "remove" them? Instead, we have these things called
>     Jeremy> "comprehensions" (which, now that I say that, seems a
>     Jeremy> rather odd name), and you can control whether they result
>     Jeremy> in a list or a generator with () or [].
> 
> Still, list comprehensions should be implemented in terms of genexps
> to get rid of the LC variable that is visible outside the scope of the
> LC.
> 

+1 . I think that we should still have the form [genexp] , but without
the dangling variable, and implemented with generator expressions. It
seems to me that it is inconsistent if I can do list(genexp) but not
[genexp] , since they are so similar. Once that happens, we can tell
people who ask the OP's question that [genexp] is just another way to
spell list(genexp), and he should use it if he wants the entire list
constructed in memory.

>     Jeremy> should be relatively simple), it's not worth breaking that
>     Jeremy> code.
> 
> Well, the code that relies on the dangling variable deserves to break.

Agreed.

Peace
Bill Mill
bill.mill at gmail.com



More information about the Python-list mailing list