在这段代码,为什么用“的”结果中没有“的StopIteration”或者是“for”循环捕获所有异常,然后默默离开? 在这种情况下,为什么我们有多余的“回报”? 或者是raise StopIteration
造成的: 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)
假设StopIteration
:正在被触发return None
。 当GeneratorExit产生?
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)
如果我手动做:
c = countdown(10)
c.close() #generates GeneratorExit??
在这种情况下,为什么我看不到回溯?