從yield說起
當(dāng)生成器執(zhí)行到y(tǒng)ield的時(shí)候抱慌,通過send方法向生成器傳遞一個(gè)值豺谈,生成器在收到傳進(jìn)來的值之后泛啸,會(huì)將其視為yield表達(dá)式的結(jié)果。之后属百,生成器執(zhí)行到下一個(gè)yield處记劝,返回yield后面的那個(gè)值給外界。
代碼一:
def my_coroutine():
while True:
received = yield
print('Received:', received)
if __name__ == '__main__':
it = my_coroutine()
next(it)
it.send('first')
it.send('second')