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

后端 未结 3 1078
滥情空心
滥情空心 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 20:37

    The yield statement used in a function turns that function into a "generator" (a function that creates an iterator). The resulting iterator is normally resumed by calling next(). However it is possible to send values to the function by calling the method send() instead of next() to resume it:

    cr.send(1)
    

    In your example this would assign the value 1 to c each time.

    cr.next() is effectively equivalent to cr.send(None)

提交回复
热议问题