python yield

`
def foo():
print("starting...")
while True:
res = yield 4 # 这里的 = 并不是将4或者返回值赋给res 而是为了让res能接受send()发送过来的参数 4作为返回值,返回给调用方(next(),send())。
print("res:", res)

foo().next() -> 接收到返回值4,res为None

foo().send(6) ==> 将6赋给res
|
v
并接收到返回值4
`

原文地址:https://www.cnblogs.com/dg-blog/p/15272224.html