天天看點

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