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

Alexander Belopolsky alexander.belopolsky at gmail.com
Mon Jan 19 19:53:36 CET 2009


On Mon, Jan 19, 2009 at 1:41 PM, Scott Dial
<scott+python-dev at scottdial.com> wrote:
> Vitor Bosshard wrote:
>> Are you even sure the list comprehension doesn't already shortcut evaluation?
>
> It does not. The body of the comprehension is evaluated all the way to
> completion, ..

In addition, the test is evaluated on all items as well:

>>> def test(i):
...    print "testing", i
...    return i**2 < 10
...
>>> a = (i for i in range(10) if test(i))
>>> a.next()
testing 0
0
>>> a.next()
testing 1
1
>>> a.next()
testing 2
2
>>> a.next()
testing 3
3
>>> a.next()
testing 4
testing 5
testing 6
testing 7
testing 8
testing 9
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration


More information about the Python-Dev mailing list