标签:yield some next return == python none 通过 例子
函数生成器中,send的作用:说明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
标签:yield some next return == python none 通过 例子
原文地址:https://blog.51cto.com/13543767/2416147