[Python-Dev] PEP 3142: Add a "while" clause to generator expressions

Paul Moore p.f.moore at gmail.com
Mon Jan 19 20:30:47 CET 2009


2009/1/19 Vitor Bosshard <algorias at yahoo.com>:
> Are you even sure the list comprehension doesn't already shortcut evaluation?
>
> This quick test in 2.6 hints otherwise:
>
>
>>>> a = (i for i in range(10) if i**2<10)

Yes, but your test, once it becomes true, remains so. Consider

>>> list(n for n in range(10) if n%2 == 0)
[0, 2, 4, 6, 8]

I assume that the intention of the while syntax is:

>>> list(n for n in range(10) while n%2 == 0)
[0]

because 1%2 != 0 so the loop stops.

Having said that, I'm -1 on the proposal. The requirement is rare
enough that the correct place for it *is* in a module - and that's
what itertools provides (along with a number of other, equally valid,
manipulations). I certainly don't see it as justifying new syntax.

Paul.


More information about the Python-Dev mailing list