[Python-ideas] "While" suggestion

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Aug 21 02:17:09 CEST 2008

Boris Borcic wrote:

> Note that you can do something like
>  >>> def yet(b) :
>     if b : raise StopIteration
>  >>> list(x for x in range(0,10) if not yet(x>4))
> [0, 1, 2, 3, 4]

However, note that this doesn't work if you use a
list comprehension instead of list(genexp).

The reason is that the StopIteration is not stopping
the for-loop, but rather the implicit iteration over
the results of the genexp being done by list().

So it only appears to work by accident.


More information about the Python-ideas mailing list