In this piece of code, why does using for
result in no StopIteration
or is the for
loop trapping all exceptions and then silently exiting?
In which case, why do we have the extraneous return
?? Or is the
raise StopIteration
caused by: return None
?
#!/usr/bin/python3.1
def countdown(n):
print("counting down")
while n >= 9:
yield n
n -= 1
return
for x in countdown(10):
print(x)
c = countdown(10)
next(c)
next(c)
next(c)
Assuming StopIteration
is being triggered by: return None
.
When is GeneratorExit
generated?
def countdown(n):
print("Counting down from %d" % n)
try:
while n > 0:
yield n
n = n - 1
except GeneratorExit:
print("Only made it to %d" % n)
If I manually do a:
c = countdown(10)
c.close() #generates GeneratorExit??
In which case why don't I see a traceback?