問題描述
在這段代碼中,為什么使用 for
導致沒有 StopIteration
還是 for
循環捕獲所有異常然后靜默退出?在這種情況下,為什么我們會有多余的 return
??或者是raise StopIteration
導致:return None
?
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)
假設 StopIteration
被觸發:return None
.GeneratorExit
是什么時候生成的?
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)
如果我手動執行:
c = countdown(10)
c.close() #generates GeneratorExit??
在這種情況下,為什么我看不到回溯?
In which case why don't I see a traceback?
推薦答案
for
循環顯式監聽 StopIteration
.
for
語句的目的是循環遍歷迭代器提供的序列,異常用于表示迭代器現在完成;for
不會捕獲被迭代的對象引發的其他異常,只是那個.
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.
這是因為 StopIteration
是正常的、預期的信號,它告訴正在迭代的任何人沒有更多的東西要產生.
That's because StopIteration
is the normal, expected signal to tell whomever is iterating that there is nothing more to be produced.
生成器函數是一種特殊的迭代器;它確實會在函數完成時引發 StopIteration
(即,當它返回時,是的,return None
引發 StopIteration
).這是迭代器的要求;他們必須在完成后提出StopIteration
;事實上,一旦一個 StopIteration
被引發,試圖從他們那里獲取另一個元素(通過 next()
,或者調用 .next()
(py 2) 或 .__next__()
(py 3) 迭代器上的方法)必須總是再次引發 StopIteration
.
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
是向other 方向通信的異常.您正在顯式關閉一個帶有 yield
表達式的生成器,Python 將該關閉傳遞給生成器的方式是在該函數內部引發 GeneratorExit
.您在 countdown
中顯式捕獲該異常,其目的是讓生成器在關閉時根據需要清理資源.
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.
GeneratorExit
不會傳播給調用者;請參閱 generator.close()
文檔一個>.
A GeneratorExit
is not propagated to the caller; see the generator.close()
documentation.
這篇關于為什么 next 會引發“StopIteration",但“for"會正常返回?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!