亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python Generators vs Iterators

系統 2090 0

http://stackoverflow.com/questions/2776829/difference-between-python-generators-vs-iterators

?

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

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 .

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.

For example, a generator such as:

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

or the equivalent generator expression (genexp)

    
      
        (
      
      
        i
      
      
        *
      
      
        i 
      
      
        for
      
      
         i 
      
      
        in
      
      
         xrange
      
      
        (
      
      
        start
      
      
        ,
      
      
         stop
      
      
        ))
      
    
  

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
      
      
        ):
      
      
        if
      
      
         self
      
      
        .
      
      
        start 
      
      
        >=
      
      
         self
      
      
        .
      
      
        stop
      
      
        :
      
      
        raise
      
      
        StopIteration
      
      
         current 
      
      
        =
      
      
         self
      
      
        .
      
      
        start 
      
      
        *
      
      
         self
      
      
        .
      
      
        start self
      
      
        .
      
      
        start 
      
      
        +=
      
      
        1
      
      
        return
      
      
         current
      
    
  

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

    
      
        def
      
      
         current
      
      
        (
      
      
        self
      
      
        ):
      
      
        return
      
      
         self
      
      
        .
      
      
        start
      
    
  

Python Generators vs Iterators


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 一区二区亚洲视频 | 99国产精品热久久久久久 | 国产一区欧美二区 | 久久国产欧美日韩精品免费 | 97国产在线视频 | 国产尤物在线播放 | 51国产午夜精品免费视频 | 久久久久久综合一区中文字幕 | 白云精品视频国产专区 | 国产精品麻豆久久99 | 久久免费视频精品 | 伊人久久综合影院 | 成人免费视频网站 | 日韩伦理视频 | 精品四虎免费观看国产高清午夜 | 久久一| 亚欧成人在线 | 丁香婷婷综合网 | 欧美专区综合 | 一本伊大人香蕉久久网手机 | 精品视频在线免费看 | 国产99精品 | 国产精品伦理一区二区三区 | 欧美成人一区二区三区不卡 | 高清在线一区二区三区亚洲综合 | 欧美综合久久 | 波多野结衣乳巨码无在线观看 | 伦理不卡| 亚洲 中文 欧美 日韩 在线人 | 亚洲精品99久久久久久 | 国产亚洲欧美日韩在线看片 | 97影院论理手机在线观看 | 亚洲高清在线视频 | 香蕉人人超 | 亚洲综合视频在线观看 | 成人欧美午夜视频毛片 | 中文字幕一区二区三 | 午夜看一级特黄a大片 | 成人一区视频 | 国产91在线播放 | 久久成人18免费 |