原文:http://pyzh.readthedocs.io/en/latest/the-python-yield-keyword-explained.html

3. (译)Python关键字yield的解释(stackoverflow)

译者: hit9
原文: http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained
译者注: 这是stackoverflow上一个很热的帖子,这里是投票最高的一个答案

3.1. 提问者的问题

Python关键字yield的作用是什么?用来干什么的?

比如,我正在试图理解下面的代码:

  1. def node._get_child_candidates(self, distance, min_dist, max_dist):
  2. if self._leftchild and distance - max_dist < self._median:
  3. yield self._leftchild
  4. if self._rightchild and distance + max_dist >= self._median:
  5. yield self._rightchild

下面的是调用:

  1. result, candidates = list(), [self]
  2. while candidates:
  3. node = candidates.pop()
  4. distance = node._get_dist(obj)
  5. if distance <= max_dist and distance >= min_dist:
  6. result.extend(node._values)
  7. candidates.extend(node._get_child_candidates(distance, min_dist, max_dist))
  8. return result

当调用 _get_child_candidates 的时候发生了什么?返回了一个链表?返回了一个元素?被重复调用了么? 什么时候这个调用结束呢?

3.2. 回答部分

为了理解什么是 yield,你必须理解什么是生成器。在理解生成器之前,让我们先走近迭代。

3.3. 可迭代对象

当你建立了一个列表,你可以逐项地读取这个列表,这叫做一个可迭代对象:

  1. >>> mylist = [1, 2, 3]
  2. >>> for i in mylist :
  3. ... print(i)
  4. 1
  5. 2
  6. 3

mylist 是一个可迭代的对象。当你使用一个列表生成式来建立一个列表的时候,就建立了一个可迭代的对象:

  1. >>> mylist = [x*x for x in range(3)]
  2. >>> for i in mylist :
  3. ... print(i)
  4. 0
  5. 1
  6. 4

所有你可以使用 for .. in .. 语法的叫做一个迭代器:链表,字符串,文件……你经常使用它们是因为你可以如你所愿的读取其中的元素,但是你把所有的值都存储到了内存中,如果你有大量数据的话这个方式并不是你想要的。

3.4. 生成器

生成器是可以迭代的,但是你 只可以读取它一次 ,因为它并不把所有的值放在内存中,它是实时地生成数据:

  1. >>> mygenerator = (x*x for x in range(3))
  2. >>> for i in mygenerator :
  3. ... print(i)
  4. 0
  5. 1
  6. 4

看起来除了把 [] 换成 () 外没什么不同。但是,你不可以再次使用 for i inmygenerator , 因为生成器只能被迭代一次:先计算出0,然后继续计算1,然后计算4,一个跟一个的…

3.5. yield关键字

yield 是一个类似 return 的关键字,只是这个函数返回的是个生成器。

  1. >>> def createGenerator() :
  2. ... mylist = range(3)
  3. ... for i in mylist :
  4. ... yield i*i
  5. ...
  6. >>> mygenerator = createGenerator() # create a generator
  7. >>> print(mygenerator) # mygenerator is an object!
  8. <generator object createGenerator at 0xb7555c34>
  9. >>> for i in mygenerator:
  10. ... print(i)
  11. 0
  12. 1
  13. 4

这个例子没什么用途,但是它让你知道,这个函数会返回一大批你只需要读一次的值.

为了精通 yield ,你必须要理解:当你调用这个函数的时候,函数内部的代码并不立马执行 ,这个函数只是返回一个生成器对象,这有点蹊跷不是吗。

那么,函数内的代码什么时候执行呢?当你使用for进行迭代的时候.

现在到了关键点了!

第一次迭代中你的函数会执行,从开始到达 yield 关键字,然后返回 yield 后的值作为第一次迭代的返回值. 然后,每次执行这个函数都会继续执行你在函数内部定义的那个循环的下一次,再返回那个值,直到没有可以返回的。

如果生成器内部没有定义 yield 关键字,那么这个生成器被认为成空的。这种情况可能因为是循环进行没了,或者是没有满足 if/else 条件。

3.6. 回到你的代码

(译者注:这是回答者对问题的具体解释)

生成器:

  1. # Here you create the method of the node object that will return the generator
  2. def node._get_child_candidates(self, distance, min_dist, max_dist):
  3.  
  4. # Here is the code that will be called each time you use the generator object :
  5.  
  6. # If there is still a child of the node object on its left
  7. # AND if distance is ok, return the next child
  8. if self._leftchild and distance - max_dist < self._median:
  9. yield self._leftchild
  10.  
  11. # If there is still a child of the node object on its right
  12. # AND if distance is ok, return the next child
  13. if self._rightchild and distance + max_dist >= self._median:
  14. yield self._rightchild
  15.  
  16. # If the function arrives here, the generator will be considered empty
  17. # there is no more than two values : the left and the right children

调用者:

  1. # Create an empty list and a list with the current object reference
  2. result, candidates = list(), [self]
  3.  
  4. # Loop on candidates (they contain only one element at the beginning)
  5. while candidates:
  6.  
  7. # Get the last candidate and remove it from the list
  8. node = candidates.pop()
  9.  
  10. # Get the distance between obj and the candidate
  11. distance = node._get_dist(obj)
  12.  
  13. # If distance is ok, then you can fill the result
  14. if distance <= max_dist and distance >= min_dist:
  15. result.extend(node._values)
  16.  
  17. # Add the children of the candidate in the candidates list
  18. # so the loop will keep running until it will have looked
  19. # at all the children of the children of the children, etc. of the candidate
  20. candidates.extend(node._get_child_candidates(distance, min_dist, max_dist))
  21.  
  22. return result

这个代码包含了几个小部分:

  • 我们对一个链表进行迭代,但是迭代中链表还在不断的扩展。它是一个迭代这些嵌套的数据的简洁方式,即使这样有点危险,因为可能导致无限迭代。candidates.extend(node._get_child_candidates(distance, min_dist, max_dist))穷尽了生成器的所有值,但 while 不断地在产生新的生成器,它们会产生和上一次不一样的值,既然没有作用到同一个节点上.
  • extend() 是一个迭代器方法,作用于迭代器,并把参数追加到迭代器的后面。

通常我们传给它一个链表参数:

  1. >>> a = [1, 2]
  2. >>> b = [3, 4]
  3. >>> a.extend(b)
  4. >>> print(a)
  5. [1, 2, 3, 4]

但是在你的代码中的是一个生成器,这是不错的,因为:

  • 你不必读两次所有的值
  • 你可以有很多子对象,但不必叫他们都存储在内存里面。

并且这很奏效,因为Python不关心一个方法的参数是不是个链表。Python只希望它是个可以迭代的,所以这个参数可以是链表,元组,字符串,生成器... 这叫做 ducktyping,这也是为何Python如此棒的原因之一,但这已经是另外一个问题了...

你可以在这里停下,来看看生成器的一些高级用法:

3.7. 控制生成器的穷尽

  1. >>> class Bank(): # let's create a bank, building ATMs
  2. ... crisis = False
  3. ... def create_atm(self) :
  4. ... while not self.crisis :
  5. ... yield "$100"
  6. >>> hsbc = Bank() # when everything's ok the ATM gives you as much as you want
  7. >>> corner_street_atm = hsbc.create_atm()
  8. >>> print(corner_street_atm.next())
  9. $100
  10. >>> print(corner_street_atm.next())
  11. $100
  12. >>> print([corner_street_atm.next() for cash in range(5)])
  13. ['$100', '$100', '$100', '$100', '$100']
  14. >>> hsbc.crisis = True # crisis is coming, no more money!
  15. >>> print(corner_street_atm.next())
  16. <type 'exceptions.StopIteration'>
  17. >>> wall_street_atm = hsbc.create_atm() # it's even true for new ATMs
  18. >>> print(wall_street_atm.next())
  19. <type 'exceptions.StopIteration'>
  20. >>> hsbc.crisis = False # trouble is, even post-crisis the ATM remains empty
  21. >>> print(corner_street_atm.next())
  22. <type 'exceptions.StopIteration'>
  23. >>> brand_new_atm = hsbc.create_atm() # build a new one to get back in business
  24. >>> for cash in brand_new_atm :
  25. ... print cash
  26. $100
  27. $100
  28. $100
  29. $100
  30. $100
  31. $100
  32. $100
  33. $100
  34. $100
  35. ...

对于控制一些资源的访问来说这很有用。

3.8. Itertools,你最好的朋友

itertools包含了很多特殊的迭代方法。是不是曾想过复制一个迭代器?串联两个迭代器?把嵌套的链表分组?不用创造一个新的链表的 zip/map?

只要 import itertools

需要个例子?让我们看看比赛中4匹马可能到达终点的先后顺序的可能情况:

  1. >>> horses = [1, 2, 3, 4]
  2. >>> races = itertools.permutations(horses)
  3. >>> print(races)
  4. <itertools.permutations object at 0xb754f1dc>
  5. >>> print(list(itertools.permutations(horses)))
  6. [(1, 2, 3, 4),
  7. (1, 2, 4, 3),
  8. (1, 3, 2, 4),
  9. (1, 3, 4, 2),
  10. (1, 4, 2, 3),
  11. (1, 4, 3, 2),
  12. (2, 1, 3, 4),
  13. (2, 1, 4, 3),
  14. (2, 3, 1, 4),
  15. (2, 3, 4, 1),
  16. (2, 4, 1, 3),
  17. (2, 4, 3, 1),
  18. (3, 1, 2, 4),
  19. (3, 1, 4, 2),
  20. (3, 2, 1, 4),
  21. (3, 2, 4, 1),
  22. (3, 4, 1, 2),
  23. (3, 4, 2, 1),
  24. (4, 1, 2, 3),
  25. (4, 1, 3, 2),
  26. (4, 2, 1, 3),
  27. (4, 2, 3, 1),
  28. (4, 3, 1, 2),
  29. (4, 3, 2, 1)]

3.9. 了解迭代器的内部机理

迭代是一个实现可迭代对象(实现的是 __iter__() 方法)和迭代器(实现的是__next__() 方法)的过程。可迭代对象是你可以从其获取到一个迭代器的任一对象。迭代器是那些允许你迭代可迭代对象的对象。

更多见这个文章 http://effbot.org/zone/python-for-statement.htm

***

yield python的更多相关文章

  1. Python: yield, python 实现tail -f

    def CreateGenerator(file): with open(file,'r') as t: t.seek(0,2) while True: line=t.readline() if no ...

  2. Python天天美味(25) - 深入理解yield

    Python天天美味(25) - 深入理解yield - CoderZh - 博客园 Python天天美味(25) - 深入理解yield   yield的英文单词意思是生产,刚接触Python的时候 ...

  3. python 全栈开发,Day113(方法和函数的区别,yield,反射)

    一.方法和函数的区别 面向对象 初级 class StarkConfig(object): def __init__(self,model_class): self.model_class = mod ...

  4. Python中yield和yield from的用法

    yield python中yield的用法很像return,都是提供一个返回值,但是yield和return的最大区别在于,return一旦返回,则代码段执行结束,但是yield在返回值以后,会交出C ...

  5. python 并发专题(四):yield以及 yield from

    一.yield python中yield的用法很像return,都是提供一个返回值,但是yield和return的最大区别在于,return一旦返回,则代码段执行结束,但是yield在返回值以后,会交 ...

  6. Python标准模块--Iterators和Generators

    1 模块简介 当你开始使用Python编程时,你或许已经使用了iterators(迭代器)和generators(生成器),你当时可能并没有意识到.在本篇博文中,我们将会学习迭代器和生成器是什么.当然 ...

  7. python基础篇

    python脚本开头 #!/usr/bin/env python# -*- coding: utf-8 -*print "你好,世界" 不要问为什么,记住就好了 变量定于的规则 变 ...

  8. 利用python yielding创建协程将异步编程同步化

    转自:http://www.jackyshen.com/2015/05/21/async-operations-in-form-of-sync-programming-with-python-yiel ...

  9. python基础(2):python的变量和常量

    今天看看python的变量和常量:python3 C:\test.py 首先先说一下解释器执行Python的过程: 1. 启动python解释器(内存中) 2. 将C:\test.py内容从硬盘读入内 ...

随机推荐

  1. MikroTik RouterOS网址资源收集

    routeros|mikrotik|ros|软路由论坛|中国路由网|软件路由|软件路由器|routeros技术论坛|路由论坛 - Powered by Discuz!   Mikrotik RB450 ...

  2. 使用CefSharp在.Net程序中嵌入Chrome浏览器(四)——启动优化

    在实际使用过程中,发现有的客户端会出现chrome加载网页过慢问题,定位后发现很多是因为设置系统代理所致,此时可以通过如下启动参数禁止系统代理. {"proxy-auto-detect&qu ...

  3. LPC43xx SGPIO Experimentation

    LPC4350 SGPIO Experimentation The NXP LPC43xx microcontrollers have an interesting, programmable ser ...

  4. TIMER门控模式控制PWM输出长度

    TIMER门控模式控制PWM输出长度 参照一些网友代码做了些修改,由TIM4来控制TIM2的PWM输出长度, 采用主从的门控模式,即TIM4输出高时候TIM2使能输出 //TIM2 PWM输出,由TI ...

  5. Revit Family API 添加几何实体

    先创建一个封闭曲线createProfileLShape();再创建实体,这里需要手工画一个参考平面; ; i < nVerts; ++i)        {            Line l ...

  6. C#各种泛型集合体验

    本篇体验除Queue<T>和Stack<T>之外的其它泛型集合. SortedList<TKey, TValue> SortedList<TKey, TVal ...

  7. mormot支持https

    mormot支持https 将ssl证书导入电脑系统,以Windows 10为例: 运行 mmc 证书导入成功后,双击证书,查看证书指纹: 第二项工作:将证书与https绑定:以管理员身份启动cmd, ...

  8. C#编程(二十六)----------泛型

    泛型 有了泛型,就可以创建独立于被包含类型的类和方法了.我们不必给不同的类型编写功能相同的许多方法或类,只创建一个方法或类即可. 另一个减少代码的选项是使用object类,但object类不是类型安全 ...

  9. C# 其他图片格式转emf

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. Facebook工程师是如何改进他们Android客户端的

    from://http://greenrobot.me/devnews/facebook-engineer-improve-android-app/ Facebook工程师是如何改进他们Android ...