生成器的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")

  

原文地址:https://www.cnblogs.com/Roc-Atlantis/p/8630025.html