what does yield as assignment do? myVar = (yield)

后端 未结 3 1071
滥情空心
滥情空心 2020-12-29 20:13

I\'m familiar with yield to return a value thanks mostly to this question

but what does yield do when it is on the right side of an assignment?

@cor         


        
3条回答
  •  遥遥无期
    2020-12-29 21:03

    You can send values to the generator using the send function.

    If you execute:

    p = protocol()
    p.next() # advance to the yield statement, otherwise I can't call send
    p.send(5)
    

    then yield will return 5, so inside the generator c will be 5.

    Also, if you call p.next(), yield will return None.

    You can find more information here.

提交回复
热议问题