[Python-ideas] "While" suggestion

Boris Borcic bborcic at gmail.com
Thu Aug 21 17:32:28 CEST 2008


Greg Ewing wrote:
> 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).

Note that this was written in the cited context
of the OP's original list comprehension.

> 
> 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
> 

...under the assumption of a specific equivalence.

Cheers, BB




More information about the Python-ideas mailing list