首页 > 其他 > 详细

yield 生成器例子

时间:2016-10-30 16:09:22      阅读:70      评论:0      收藏:0      [点我收藏+]
 1 #!/usr/bin/env python
 2 #encoding: utf-8
 3 import time
 4 def consumer(name):
 5     print (%s 来吃包子了。。。 % (name))
 6     while True:
 7         baizi = yield  #执行到这会暂停,直到调用next的方法,然后在从这里执行
 8         print ("包子 [%s] 来了, 被 [%s] 吃了,,," % (baizi,name))
 9 
10 def producer():
11     c1 = consumer(lys)  #生成迭代器
12     c2 = consumer(zhy)
13     c1.__next__()  #迭代
14     c2.__next__()
15     for i in range(3):
16         print (开始重新做包子了,,,)
17         time.sleep(1)
18         print (做好了2个。来吃吧,,,)
19         c1.send(i) #发送给yied,接收参数
20         c2.send(i)
21 
22 producer()

 

yield 生成器例子

原文:http://www.cnblogs.com/liuyoushui/p/6013108.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!