python generation formula send of method of details
- 2020-05-30 20:33:52
Look for casually on the net, the feeling is to speak along while speak not clear, write 1 here.
def generator(): while True: receive=yield 1 print('extra'+str(receive)) g=generator() print(next(g)) print(g.send(111)) print(next(g))
1 extra111 1 extraNone 1
Why is that? Click on send and you get 1 sentence
send: Resumes the generator and "sends" a value becomes the result of current yield expression
So yield 1 as a whole is treated as an expression, your send content is going to be the value of this expression, whatever you want to take or not take to the left, but yield is the thing that your send came in with. This expression becomes what you had when send came in and then continues to execute, again encountering yield, and printing yield followed by the expression.
Of course, normally you don't output a constant, you output a quantity that is related to what you receive, otherwise you just send it away.