Learning Python 019 生成器(Generators)和 yield

来源:互联网 发布:java元注解 编辑:程序博客网 时间:2024/04/27 04:25

  • 使用的电脑系统:Windows 10 64位
  • 使用的开发集成环境:PyCharm 2016.1.4
  • 使用的Python的版本:python 2.7.10 和 python 3.5.0

知识点:生成器

生成器只能用于迭代操作。

一个函数,其中带 yield 关键字的代码,它不会执行,只是记下有这个操作;其他代码正常的执行。而被记下的这些操作会像队列一样存起来,这个“队列”就是 生成器,并且会类似于return一样返回。

一个函数的代码里面有 yield 关键字,那么这个函数就是一个制造生成器的函数

生成器是Python中的高级特性。我之前学习过,还写了一个博客:Learning Python 011 高级特性 2


Python3 例子

>>> def f():...     yield 1...     yield 2...     yield 3...
>>> g = f()>>> g<generator object f at 0x0000023B63151258>>>> type(g)<class 'generator'>>>> next(g)1>>> next(g)2>>> next(g)3>>> next(g)Traceback (most recent call last):  File "<stdin>", line 1, in <module>StopIteration>>> list(g)[]>>> g = f()>>> list(g)[1, 2, 3]>>> list(g)[]>>>

>>> def counter(num):...     print('Run to the ' + str(num) + ' yield')...>>> def f():...     print('start!')...     yield counter(1)...     yield counter(2)...     yield counter(3)...     print('Done!')...
>>> g = f()>>> g<generator object f at 0x000001D4843E1258>>>> next(g)start!Run to the 1 yield>>> next(g)Run to the 2 yield>>> next(g)Run to the 3 yield>>> next(g)Done!Traceback (most recent call last):  File "<stdin>", line 1, in <module>StopIteration>>> list(g)[]>>> g = f()>>> list(g)start!Run to the 1 yieldRun to the 2 yieldRun to the 3 yieldDone![None, None, None]>>>

Python2 实例

生成器在 python2 的用法和在python3中的用法一样,唯一的区别是:

  • python2 中 可以使用next(g) 或者 g.next()。 这样两个等价
  • python3 中 只有next(g),没有g.next()

参考网站:生成器


请访问: http://www.aobosir.com/

0 0
原创粉丝点击