Nick Coghlan wrote:
list(x for x in range(10) if x < 5 or return) [0, 1, 2, 3, 4]
This is emphatically *not* executable pseudocode - it only makes sense in terms of the translation to the corresponding full generator function
-1 on messing with the semantics of 'return' (which would have implications beyond this use case) just so you can write something in one particular context that doesn't read intuitively. If early exit from comprehensions is really a desirable feature (and Guido doesn't seem to think so) it would be better addressed with an explicit language feature, e.g. list(x for x in range(10) while x < 5) [x for x in range(10) while x < 5] This makes the intent perfectly clear and works just as well in all kinds of comprehensions. It breaks the strict equivalence between comprehension syntax and the corresponding nested loop code, but you can't have everything. -- Greg