21 Aug
2008
21 Aug
'08
6:32 p.m.
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