Preliminary understanding of yield and yield

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.