Python yield and generators (generator)

Source: Internet
Author: User

In python, yield is such a generator.

Yield generator running mechanism:
When
When you ask the generator for a number, the generator will execute until the yield statement appears. The generator will give you the yield parameter, and then the generator will not continue to run.
When you ask him for the next number, he will go from the last status. Run until the yield statement is displayed. Give the parameter to you and then stop. So repeatedly
Until you exit the function. (Here is a simple example of yield ).

Yield usage:
In python, When you define a function and use the yield keyword, this function is a generator and its execution will be different from other common functions, the function returns an object instead of the return statement that you normally use to obtain the result value. To obtain the value, you must call the next () function, for example:

C = H () # H () contains the yield keyword.
# Return Value
Print C. Next ()

For example, some for in operations will automatically call the. Next () method of the generator.

Every time the next function of the iterator is called, when the generator function runs to yield, the value after yield is returned and paused at this place, all the statuses will be kept, wait until the next function is called or an exception loop is triggered to exit.

Next, let's take a look at the following exampleCodeIt is used to describe the yield operating mechanism.

# Encoding = UTF-8

Def FIB ( Max ):
A , B = 1 , 1
While A < Max :
Yield A # Generators return an iterator that returns a stream of values.
A , B = B , A + B

ForNIn FIB(15):
PrintN

You can find out from the previous Running Mechanism description,ProgramWhen you run the command to yield, the execution will not continue. Returns an iterator object that contains all parameters of the current function. The purpose is to obtain the value of all parameters that can be accessed to the function for the second call, instead of re-assigning values.

When the program is called for the first time:

Yield A # at this moment, the values of A and B are respectively. Of course, the program is also running to stop and return.

Cheng
For the second call in sequence:
As we can see from the previous figure, when a, B = is called for the first time, the program jumps to the next () method of the iterator object returned for the first time.
Run the statements a, B = B, A + B at the yield statement. The value changes to a, B = 1, (1 + 1) => A, B = 1, 2
The program continues the while loop.
Statement. It is also the first time that the status of all parameters of the function is saved, and an iterator object containing the status of these parameters is returned.

Wait for the third call.

Through the above analysis, the detailed operation process of yield can be displayed one by one!

Related Article

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.