首页 / PYTHON / Python 深入理解yield
Python 深入理解yield
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python 深入理解yield,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3806字,纯文字阅读大概需要6分钟。
内容图文
![Python 深入理解yield](/upload/InfoBanner/zyjiaocheng/1054/5c5585bcc20c4e25af9b07d9b54df556.jpg)
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->def addlist(alist):
for i in alist:
yield i + 1
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->alist = [1, 2, 3, 4]
for x in addlist(alist):
print x,
1. 包含yield的函数
假如你看到某个函数包含了yield,这意味着这个函数已经是一个Generator,它的执行会和其他普通的函数有很多不同。比如下面的简单的函数:Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->def h():
print ‘To be brave‘
yield 5
h()
2. yield是一个表达式
Python2.5以前,yield是一个语句,但现在2.5中,yield是一个表达式(Expression),比如:Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->m = yield 5
3. 透过next()语句看原理
现在,我们来揭晓yield的工作原理。我们知道,我们上面的h()被调用后并没有执行,因为它有yield表达式,因此,我们通过next()语句让它执行。next()语句将恢复Generator执行,并直到下一个yield表达式处。比如:Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->def h():
print ‘Wen Chuan‘
yield 5
print ‘Fighting!‘
c = h()
c.next()
Wen Chuan
当我们再次调用c.next()时,会继续执行,直到找到下一个yield表达式。由于后面没有yield了,因此会拋出异常:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->Wen Chuan
Fighting!
Traceback (most recent call last):
File "/home/evergreen/Codes/yidld.py", line 11, in <module>
c.next()
StopIteration
4. send(msg) 与 next()
了解了next()如何让包含yield的函数执行后,我们再来看另外一个非常重要的函数send(msg)。其实next()和send()在一定意义上作用是相似的,区别是send()可以传递yield表达式的值进去,而next()不能传递特定的值,只能传递None进去。因此,我们可以看做c.next() 和 c.send(None) 作用是一样的。
来看这个例子:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->def h():
print ‘Wen Chuan‘,
m = yield 5 # Fighting!
print m
d = yield 12
print ‘We are together!‘
c = h()
c.next() #相当于c.send(None)
c.send(‘Fighting!‘) #(yield 5)表达式被赋予了‘Fighting!‘
Wen Chuan Fighting!
需要提醒的是,第一次调用时,请使用next()语句或是send(None),不能使用send发送一个非None的值,否则会出错的,因为没有yield语句来接收这个值。
5. send(msg) 与 next()的返回值
send(msg) 和 next()是有返回值的,它们的返回值很特殊,返回的是下一个yield表达式的参数。比如yield 5,则返回 5 。到这里,是不是明白了一些什么东西?本文第一个例子中,通过for i in alist 遍历 Generator,其实是每次都调用了alist.Next(),而每次alist.Next()的返回值正是yield的参数,即我们开始认为被压进去的东东。我们再延续上面的例子:Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->def h():
print ‘Wen Chuan‘,
m = yield 5 # Fighting!
print m
d = yield 12
print ‘We are together!‘
c = h()
m = c.next() #m 获取了yield 5 的参数值 5
d = c.send(‘Fighting!‘) #d 获取了yield 12 的参数值12
print ‘We will never forget the date‘, m, ‘.‘, d
Wen Chuan Fighting!
We will never forget the date 5 . 12
6. throw() 与 close()中断 Generator
中断Generator是一个非常灵活的技巧,可以通过throw抛出一个GeneratorExit异常来终止Generator。Close()方法作用是一样的,其实内部它是调用了throw(GeneratorExit)的。我们看:Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->def close(self):
try:
self.throw(GeneratorExit)
except (GeneratorExit, StopIteration):
pass
else:
raise RuntimeError("generator ignored GeneratorExit")
# Other exceptions are not caught
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->Traceback (most recent call last):
File "/home/evergreen/Codes/yidld.py", line 14, in <module>
d = c.send(‘Fighting!‘) #d 获取了yield 12 的参数值12
StopIteration
注:以上观点属于本人的个人理解,如有偏差请批评指正。谢谢!
原文:http://www.jb51.net/article/15717.htm
内容总结
以上是互联网集市为您收集整理的Python 深入理解yield全部内容,希望文章能够帮你解决Python 深入理解yield所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。