On 11/20/2014 04:01 AM, Nick Coghlan wrote:
Can currently be converted to a generator expression with the aid of a helper function:
... raise StopIteration ...
list(x for x in range(10) if x < 5 or stop())
[0, 1, 2, 3, 4]
How about making a stop_generator() function builtin, just for this feature?
It could raise a specific StopIteration instance, possibly the same subclass/instance as return does in generators, so the generator would know to catch it instead of complaining. As a builting, all the mechanics are kept out of the way as implementation details.
For those who already use stop() in many places, if there are any one who does that often, they could just do stop = stop_generator at the top of their programs.
I think "stop" is too general a name in this case. It's fine when the function is defined right above where it is used, but as a builtin a more specific name would be better.