Python generators (generator)--yield

Source: Internet
Author: User

Reference: http://blog.csdn.net/scelong/article/details/6969276Python Builder

What is a python generator, meaning a function with a yield statement, since it is a function, what does it have to do with a normal function?

The generator is a function that remembers where in the function body the last time it was returned. A second (or nth) call to the generator function jumps to the middle of the function, and all local variables that were last called remain unchanged.

The generator not only "remembers" its data state; the generator also "remembers" its location in flow control constructs (in imperative programming, this construct is not just a data value).

features of the generator :

The generator is a function, and the parameters of the function are preserved.

When iterating to the next invocation, the parameters used are left for the first time, that is, the parameters of all function calls are preserved the first time they are called, not the newly created

The yield generator's operating mechanism:

When you ask the generator to take a number, the generator executes until the yield statement appears, and the generator gives you the yield argument, and the generator does not go down. When you ask him for the next number, he will be from the last state. Start running until the yield statement appears, give you the parameters, and then stop. So repeat until you exit the function.

Cases:

1 def fib (max): 2     A, B = 1, 13while      a < Max:4         yield#  Generators return an iterator that returns a stream of values. 5         A, B = B, a+b

Program run:

1  for  in fib:2     print n

From the previous description of the operating mechanism, it can be learned that the program runs to yield this line, will not continue to execute. Instead, it returns a iterator object that contains the state of all the parameters of the current function . The goal is to be able to access all of the parameter values of the function when it is called for the second time, rather than re-assigning the value at the first visit.

So the result is the output:

1            //First call 1//            at this time A is 1,b for a            //And so on 35813

The first time the program is called:

1 def fib (max): 2     A, B = 1, 13while      a < Max:4         yield# at this point A, B value is 1, 1, of course, the program is also executed to this point, return 5         A, B = B, a+b

When the program is called for the second time:

From the front, the first call, a,b=1,1, then, when we call the second time (in fact, is called the first return of the iterator object of the next () method), the program jumps to the yield statement,

Executes a B = b, a+b statement, when the value changes to: A, B = 1, (+) = A, A, = 1, 2

The program continues the while loop, and, of course, once again touches the yield a statement, which, like the first time, saves the state of all parameters of the function and returns a iterator object containing the state of those parameters.

Wait for the third call:

1 def fib (max): 2     A, B = 1, 13      while a < Max:4         yield  A//At this time A is the last saved 2
    5         A, B = B, a+b

The following is the process of doing it in turn!

The difference between a generator and an iterator:

The generator is an iterator. The generator has the next method and behaves exactly the same as the iterator, which means that the generator can also be used in the Python for loop.

In the python for loop, there is a next () Call and processing of the stopiteration, so that the generator and the iterator run appear to be the same, as follows:

1iterator = [I forIinchRange (5)]2  forObjinchiterator:3     Printobj,4     #0 1 2) 3 45 6Generator = (i forIinchRange (5))7  forObjinchGenerator:8     Printobj,9     #0 1 2) 3 4

When you use a list build to create a list, you create an iterative object that appears to be nothing different than replacing [] with (). However, you cannot use the for I in Mygenerator again, because the generator can only be dropped once: Calculate 0 first, then continue to calculate 1, then calculate 4, one with one.

Note: All you can use for. In.. The syntax is called an iterator: Linked list, string, file ... You often use them because you can read the elements as you wish, but you store all the values in memory, which is not what you want if you have a lot of data.

Python generators (generator)--yield

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.