Python generator:next, sent (msg) differences

Source: Internet
Author: User

For a normal generator, the first next call, equivalent to the start generator, executes from the first line of code of the generator function, until the first execution of the yield statement (line 4th), jumps out of the generator function.

Then the second next call, after entering the generator function, starts execution from the next sentence of the yield statement (line 5th), then runs back to the yield statement, executes, jumps out of the generator function,

Call next again later, and so on. Here is a few:

Consumer (): 2     ' here '3     xrange (3): 4         R5         ' OK ' + str (i)
6
7 C = Consumer () 8 N1 = C.next () 9 N2 = C.next () n3 = C.next ()

Once we see how next () is going to make the function that contains yield, let's look at another very important function, send (msg). In fact, next () and send () function in a certain sense is similar, the difference is that send () can pass the value of the yield expression in, and next () cannot pass a specific value, can only pass the none in. Therefore, we can be seen as C.next () and C.send (None) are the same. It should be recalled that the first call, using the next () statement or send (None), cannot use Send to send a value other than None, otherwise it will be wrong, because there is no Python yield statement to receive this value.
The next step is to highlight the order in which send executes. When the first send (None), which corresponds to 11 rows, starts the generator, executes from the first line of code of the generator function, and jumps out of the generator function after the first execution of yield (corresponding to line 4th). In this process, N1 has not been defined.

Following the run to send (1), go to the generator function, note that this is different from calling next. This is done from line 4th and assigns 1 to N1, but does not perform the yield portion. Continue from the next statement of yield, and then rerun to the yield statement, after execution, jump out of the generator function.

That is, send and next are just the beginning of an assignment, and the other runs are the same.

Consumer (): 2     ' here '3     true:4         R5         n1:6              Return7         print(% N1) 8         ' Total OK '+str (N1) 9Produce (c): 11     AA = C.send (None)     n = 013     N < 5:14         n = n +         print(% n)         r1 = c.send (n)         print(% R1)     c.close () 1920x1080 C = consumer ()Produce (c) 

Python generator:next, sent (msg) differences

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: info-contact@alibabacloud.com 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.