Short-circuiting iterators
Hello, I've not had much luck in searching for a discussion on this in the Python-Dev archives, so bear with me. I had an idea this morning for a simple extension to Python's iterator protocol that would allow the user to force an iterator to raise StopIteration on the next call to next(). My thought was to add a new method to iterators called stop(). In my situation it would be useful as a control-flow mechanism, but I imagine there are many other use cases for it: generator = some_generator_function() for x in generator: ... deeply ... ... nested ... ... control-flow ... if satisfaction_condition: # Terminates the for-loop, but # finishes the current iteration generator.stop() ... more stuff ... I'm curious if anything like this has been proposed in the past. If so, could someone kindly point me to any relevant mailing list threads? Matthew Barnes
On 11/30/05, Matthew F. Barnes
I'm curious if anything like this has been proposed in the past. If so, could someone kindly point me to any relevant mailing list threads?
PEP 342, already accepted and found at http://python.org/peps/pep-0342.html , covers related functionality (as well as many other points). Akex
On Wed, 2005-11-30 at 10:57 -0800, Alex Martelli wrote:
PEP 342, already accepted and found at http://python.org/peps/pep-0342.html , covers related functionality (as well as many other points).
Thanks Alex, I'll take another look at that PEP. The first time I tried to read it my brain started to sizzle. I happened to use a generator-iterator in my example, but my thought was that the extension could be applied to iterators in general, including sequence-iterators. Matthew Barnes
participants (2)
-
Alex Martelli
-
Matthew F. Barnes