Preliminary understanding of yield and yield
1 def go (): 2 while True: 3 data = 1 4 r = yield data # data is the returned value, r is the received value 5 print ("data", data) 6 print ("A1", r) 7 data + = 1 8 9 r = yield data10 print ("data", data) 11 r + = r12 print ("A2 ", r) 13 data + = 114 15 r = yield data16 print ("data", data) 17 print ("A3", r) 18 # If the next yield cannot be found after the runtime, the StopIteration19 20 my = go () 21 print ("my", my) 22 print ("None", my. send (None) 23 print (my. send ("1") 24 print (my. send ("2") 25 print (my. send ("3 "))
View Code
① My. send (None): indicates to start the coroutine. In this step, the data value after the first yield is returned.
② Send once. The Running code is the code between two yield instances. Finally, the data value after the last yield is returned. If the last yield statement is missing, the error "Stoplteration" will be reported"
③ R = yield data
R is the data passed in by my. send ('Incoming data ').
Data returned after this segment is run
Thank you for your comments.