生成器的send方法

send 和next区别

next:唤醒并继续执行

send:唤醒并继续执行

   发送信息到生成器内部。

def fib(max):
    n,a,b = 0,0,1
    while n < max:
        msg = yield b   #yield b 赋值给msg,msg接收send来的信号。
        print(msg)
        if msg == "stop":
            break

        a,b = b,a+b
        n += 1

f = fib(15)           #生成器对象
# for i in f:
#     print(i)
next(f)
f.send("stop")

  

posted @ 2018-03-23 14:11  Roc_Atlantis  阅读(255)  评论(0编辑  收藏  举报