Different behaviour in list comps and generator expressions

Dan Stromberg drsalists at gmail.com
Mon Nov 17 18:38:59 EST 2014


On Mon, Nov 17, 2014 at 3:30 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
>> The following list comprehension and generator expression are almost, but
>> not quite, the same:
>>
>> [expr for x in iterable]
>>
>> list(expr for x in iterable)
>>
>>
>> The difference is in the handling of StopIteration raised inside the expr.
> [...]
>
>
> Thanks to Roy and Wolfgang for their comments.
>
> If anyone is interested, Guido will soon be ruling on a PEP which will
> change the behaviour of generators and generator expressions, and he has
> asked for feedback. In particular, examples of code which will be broken by
> this change.
>
> Read the PEP here:
>
> https://www.python.org/dev/peps/pep-0479
>
>
> If you post comments here, I will see that they get forwarded on.

I'm inclined to say that list comprehensions and generator expressions
should be different.  I don't really think they should be identical,
one being eager and one being lazy.  Why let the implementation detail
of one impact the other?



More information about the Python-list mailing list