Given a simple generator:
def myGenerator(max):
for i in range(max):
yield i
It can be used like:
>>> gen = myGenerator(10)
>>> next(gen)
0
>>> next(gen)
1
When I execute close()
on the generator, all subsequent calls to next
result in a StopIteration
exception.
>>> gen.close()
>>> next(gen)
StopIteration exception
Can the generator take notice on that? yield
does not throw an exception. I'm looking for something like:
def myGenerator(max):
for i in range(max):
try:
yield i
except CloseIteration as ex:
print("I got closed")