说明t.send("stop")把括号里的“stop”传到函数里的sign了。
所以可以通过判断传给sign的值来决定要不要终止函数内的循环:
def range(n):
count=0
while count<n:
print("count",count)
count+=1
sign=yield count
if sign=="stop":
break(或者return也一样)
print("------sign",sign)
t=range(3)
next(t)
print("i will go to do somethingeles")
t.send("stop")
send
PYTHON学习0038:函数---生成器send方法---2019-7-2
原文:https://blog.51cto.com/13543767/2416147