久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么 next 會引發“StopIteration",但“for&qu

Why does next raise a #39;StopIteration#39;, but #39;for#39; do a normal return?(為什么 next 會引發“StopIteration,但“for會正常返回?)
本文介紹了為什么 next 會引發“StopIteration",但“for"會正常返回?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在這段代碼中,為什么使用 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产成人高清成人av片在线看 | 国产乱码久久久久久 | 韩日视频在线观看 | 日韩精品一区二区三区中文在线 | 日韩视频免费 | 国产精品v| 欧美日韩中文在线 | 亚洲欧美日韩中文在线 | 在线黄色影院 | 91精品国产综合久久久动漫日韩 | 国产女人与拘做受免费视频 | 久艹av| 欧美一级毛片在线播放 | 国产欧美精品一区二区三区 | 国产在线看片 | 手机av免费在线 | 国产精品日日夜夜 | 精品在线播放 | 99久久精品国产一区二区三区 | 一区二区三区免费观看 | 欧美视频一区二区三区 | 亚洲国产中文字幕 | 国产一区二区在线视频 | 亚洲色图婷婷 | 成人免费视频网站在线看 | 精品国产久| 亚洲成人三区 | 欧美精品一区二区三区四区五区 | 久久精品一区 | 毛片久久久 | 黄色片免费在线观看 | 国产免费视频 | 国产中文视频 | av一级| 五月婷婷 六月丁香 | 99色播| 欧美日韩在线综合 | 亚洲一区二区三区免费 | 人人干超碰 | 国产91黄色 | 一区二区三区中文字幕 |