python yield generator 详解

在python的函数(function)定义中,只要出现了yield表达式(Yield expression),那么事实上定义的是一个generator function, 调用这个generator function返回值是一个generator

>>> def gen_example():
...     print 'before any yield'
...     yield 'first yield'
...     print 'between yields'
...     yield 'second yield'
...     print 'no yield anymore'
... 
>>> gen = gen_example()
>>> gen.next()    # 第一次调用next
before any yield
'first yield'
>>> gen.next()    # 第二次调用next
between yields
'second yield'
>>> gen.next()    # 第三次调用next
no yield anymore
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteratio

调用gen_example方法并没有输出任何内容,说明函数体的代码尚未开始执行。当调用generator的next方法,generator会执行到yield 表达式处,返回yield表达式的内容,然后暂停(挂起)在这个地方,所以第一次调用next打印第一句并返回“first yield”。 暂停意味着方法的局部变量,指针信息,运行环境都保存起来,直到下一次调用next方法恢复。第二次调用next之后就暂停在最后一个yield,再次调用next()方法,则会抛出StopIteration异常。 

因为for语句能自动捕获StopIteration异常,所以generator(本质上是任何iterator)较为常用的方法是在循环中使用.

generator function产生的generator与普通的function有什么区别呢

  (1)function每次都是从第一行开始运行,而generator从上一次yield开始的地方运行

  (2)function调用一次返回一个(一组)值,而generator可以多次返回

  (3)function可以被无数次重复调用,而一个generator实例在yield最后一个值 或者return之后就不能继续调用了

References:

  1. python yield generator 详解

posted @ 2020-05-06 01:02  keeps_you_warm  阅读(191)  评论(0编辑  收藏  举报