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

Python 的生成器和迭代器的區(qū)別

Difference between Python#39;s Generators and Iterators(Python 的生成器和迭代器的區(qū)別)
本文介紹了Python 的生成器和迭代器的區(qū)別的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

迭代器和生成器有什么區(qū)別?關(guān)于何時(shí)使用每種情況的一些示例會(huì)很有幫助.

What is the difference between iterators and generators? Some examples for when you would use each case would be helpful.

推薦答案

iterator 是一個(gè)更籠統(tǒng)的概念:任何類具有 __next__ 方法(Python 2 中的 next) 和一個(gè) __iter__ 方法,該方法執(zhí)行 return self.

iterator is a more general concept: any object whose class has a __next__ method (next in Python 2) and an __iter__ method that does return self.

每個(gè)生成器都是一個(gè)迭代器,但反之則不然.生成器是通過調(diào)用具有一個(gè)或多個(gè) yield 表達(dá)式(yield 語句,在 Python 2.5 及更早版本中)的函數(shù)來構(gòu)建的,并且是滿足上一段定義的對(duì)象迭代器.

Every generator is an iterator, but not vice versa. A generator is built by calling a function that has one or more yield expressions (yield statements, in Python 2.5 and earlier), and is an object that meets the previous paragraph's definition of an iterator.

當(dāng)您需要一個(gè)具有某種復(fù)雜的狀態(tài)維護(hù)行為的類,或者想要公開除 __next__ (和 __iter____init__).大多數(shù)情況下,一個(gè)生成器(有時(shí),對(duì)于足夠簡單的需求,一個(gè)生成器表達(dá)式)就足夠了,而且它更容易編碼,因?yàn)闋顟B(tài)維護(hù)(在合理的范圍內(nèi))基本上是為你完成"的.由框架暫停和恢復(fù).

You may want to use a custom iterator, rather than a generator, when you need a class with somewhat complex state-maintaining behavior, or want to expose other methods besides __next__ (and __iter__ and __init__). Most often, a generator (sometimes, for sufficiently simple needs, a generator expression) is sufficient, and it's simpler to code because state maintenance (within reasonable limits) is basically "done for you" by the frame getting suspended and resumed.

例如生成器如:

def squares(start, stop):
    for i in range(start, stop):
        yield i * i

generator = squares(a, b)

或等效的生成器表達(dá)式(genexp)

or the equivalent generator expression (genexp)

generator = (i*i for i in range(a, b))

需要更多代碼來構(gòu)建自定義迭代器:

would take more code to build as a custom iterator:

class Squares(object):
    def __init__(self, start, stop):
       self.start = start
       self.stop = stop
    def __iter__(self): return self
    def __next__(self): # next in Python 2
       if self.start >= self.stop:
           raise StopIteration
       current = self.start * self.start
       self.start += 1
       return current

iterator = Squares(a, b)

但是,當(dāng)然,使用類 Squares 您可以輕松地提供額外的方法,即

But, of course, with class Squares you could easily offer extra methods, i.e.

    def current(self):
       return self.start

如果您對(duì)應(yīng)用程序中的此類額外功能有任何實(shí)際需求.

if you have any actual need for such extra functionality in your application.

這篇關(guān)于Python 的生成器和迭代器的區(qū)別的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 在线一区观看 | 五月婷婷中文 | 国产色| 五月精品视频 | 欧美一级久久久猛烈a大片 日韩av免费在线观看 | 日韩黄色小视频 | 国产一级淫片a直接免费看 免费a网站 | 久草www| 一区二区高清不卡 | 中文天堂在线一区 | 久草新在线| 91直接看| 亚洲精品二区 | 99精品久久久 | 在线免费观看成人 | 在线视频 中文字幕 | 欧美中文字幕在线观看 | 91久久久久 | 国产成人a亚洲精品 | 日本aⅴ中文字幕 | 国产欧美日韩一区二区三区在线观看 | 欧美日韩大片 | 欧美 日韩 亚洲91麻豆精品 | 情侣酒店偷拍一区二区在线播放 | 亚洲精品在线观看网站 | 午夜免费视频 | 成人免费视频网站在线观看 | 久久久.com | 日本在线一区二区三区 | 99国产精品久久久久久久 | 国产一区在线免费观看 | 91极品视频| 中文字幕国产一区 | 亚洲成人av一区二区 | 插插插干干干 | 国产精品一区一区 | www.v888av.com| 成人二区 | 黄色一级免费观看 | 大伊人久久| 在线综合视频 |