> 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(). There's no need to change the iterator protocol for your example use case; you could just define a simple iterator-wrapper: class InterruptableIterator: stopped = False def __init__(self, iter): self.iter = iter() def next(self): if stopped: raise StopIteration('iterator stopped.') return self.iter.next() def stop(self): self.stopped = True And then just replace: > generator = some_generator_function() with: generator = InterruptableIterator(some_generator_function()) -Edward
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4