1 # _*_coding:utf-8_*_
 2 '''
 3 如何发送信号终止生成器呢?  ======> send方法
 4 '''
 5 def demo(n):
 6     count = 0
 7     while count < n:
 8         print("count: ",count)
 9         count += 1
10         sign = yield count
11         print("sign: ",sign)
12         if sign == "stop":
13             break
14         # print("sign....",sign)
15     return "异常值"
16 
17 g = demo(3)
18 # n1 = next(g)
19 g.send(None)
20 
21 
22 
23 # 1.next方法可看成向yield发送了一个None值
24 # 2.生成器刚开始启动时,必须用send或者next方法发送None值启动,
25 """
26 def demo(n):
27     count = 0
28     while count < n:
29         print("count: ",count)
30         count += 1
31         sign = yield count
32         # print("sign: ",sign)
33         # if sign == "stop":
34         #     break
35         print("sign....",sign)
36     # return "异常值"
37 
38 g = demo(3)
39 # n1 = next(g)
40 g.send("stop")
41 g.send(None)
42 """
43 #3.next会发送None值和唤醒并执行生成器,而send可以发送其他信号到生成器内部,发送None时,会唤醒并执行生成器

 

posted on 2019-07-05 17:37  嗯哈哈  阅读(265)  评论(0编辑  收藏  举报