has just written an article about yield, which can be referenced by the landlord.
python-material-collection/blob/master/examples/yield.md" rel=" nofollow noreferrer "> what exactly does it do when we call yield,
write something about my understanding:
when sq.send (7) is called for the first time, the code executes to:
response=yield cursor**2
at this point, the value of response is 7, and then execute
cursor+=1 -sharp cursor3
then go on to the next loop, at this time:
if response:
response = yeild response*2
returns 49, and then when you type next (sq), response is not explicitly assigned, so response becomes None; and then runs:
continue
then go down normally.
1 def squares(cursor=1):
2 response=None
3 while True:
4 if response:
5 response=yield response**2
6 continue
7 response=yield cursor**2
8 cursor+=1
(1) the first call to next (sq), hangs at position 7, waiting for you to pass a value through send, and the returned cursor**2, is 1.
(2) when you call sq.send (7), the generator hangs at position 7, passing in 7, that is, assigning a value of 7 to response, loop, arriving at position 4, entering the if statement and returning 7 7, that is, 49. The generator hangs at position 5 and calls next (sq), continue, again to return the first sentence of the loop, that is, position 3. Without passing send, the generator assigns None to the response, with a value of 7. Finally, as above, it hangs at position 7, and returns the previous cursor 2, that is, 9
.