为什么 next 会引发“ StopIteration”,而“ for”会执行正常的返回?

在这段代码中,为什么使用 for会导致没有 StopIteration 还是 for循环捕获所有异常,然后悄悄退出? 在这种情况下,为什么我们有无关的 return? ? 或者是 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??

这样的话,为什么我看不到追踪记录?

143279 次浏览

The for loop listens for StopIteration explicitly.

The purpose of the for statement is to loop over the sequence provided by an iterator and the exception is used to signal that the iterator is now done; for doesn't catch other exceptions raised by the object being iterated over, just that one.

That's because StopIteration is the normal, expected signal to tell whomever is iterating that there is nothing more to be produced.

A generator function is a special kind of iterator; it indeed raises StopIteration when the function is done (i.e. when it returns, so yes, return None raises StopIteration). It is a requirement of iterators; they must raise StopIteration when they are done; in fact, once a StopIteration has been raised, attempting to get another element from them (through next(), or calling the .next() (py 2) or .__next__() (py 3) method on the iterator) must always raise StopIteration again.

GeneratorExit is an exception to communicate in the other direction. You are explicitly closing a generator with a yield expression, and the way Python communicates that closure to the generator is by raising GeneratorExit inside of that function. You explicitly catch that exception inside of countdown, its purpose is to let a generator clean up resources as needed when closing.

A GeneratorExit is not propagated to the caller; see the generator.close() documentation.