Generator expressions vs. comprehensions
Peter Otten
__peter__ at web.de
Tue May 25 03:08:56 EDT 2010
Michele Simionato wrote:
> On May 25, 12:47 am, Carl Banks <pavlovevide... at gmail.com> wrote:
>> The situation here is known. It can't be corrected, even in Python 3,
>> without modifying iterator protocol to tie StopIteration to a specific
>> iterator. This is possible and might be worth it to avoid hard-to-
>> diagnose bugs but it would complicate iterator protocol, which becomes
>> less useful as it becomes more complex.
>
> The situation here is a known and could be corrected by changing the
> meaning of list comprehension,
> for instance by having [x for x in iterable] to be an alias for list(x
> for x in iterable). In such a way the StopIteration exception would be
> always swallowed and there would be consistency with generator
> expressions (by construction). However, the list comprehension would
> become non-equivalent to the corresponding for-loop with an .append,
> so somebody would be un happy anyway :-/
But the list comprehension is already non-equivalent to the for loop as the
loop variable isn't leaked anymore. We do have three similar constructs with
subtle differences.
I think not turning the list-comp into syntactic sugar for list(genexp) in
py3 is a missed opportunity.
Peter
More information about the Python-list
mailing list