Deep understanding of Python Builder (Generator)

Source: Internet
Author: User
Tags function definition generator stdin

We can create a list simply by creating a list, but with memory limitations, the list size is certainly limited. Also, creating a list of 1 million elements takes up a lot of storage space, and if we just need to access the first few elements, then the vast majority of the space behind it is wasted.

So, if the list element can be calculated according to an algorithm, can we continue to calculate the subsequent elements in the process of the loop? This eliminates the need to create a complete list, which saves a lot of space. In Python, this side loop computes the mechanism, called the Generator (Generator).

There are a number of ways to create a generator. The first method is simple, as long as a list-generated [] is changed to (), a generator is created:

123456 >>> mylist = [ x forin range(1, 10)] >>> mylist [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> gen = (x for inrange(1,10)) >>> gen <generator object <genexpr> at 0x7f1d7fd0f5a0>

The difference between creating MyList and Gen is only the outermost [] and (), MyList is a list, and Gen is a generator (generator).

We can print out every element of the list directly, but how do we print out every element of generator?

If you want one print out, you can go through the generator Next () method:

12345678910111213 >>>  gen.next () 1 >>> gen.next () 2 >>> gen.next () 3 >>> gen.next () 9 >>> gen.next () traceback  (most recent call last): &NBSP;&NBSP; file  "<stdin>" , line 1,  in  <module> stopiteration

As we have said, generator holds the algorithm, each time it calls next (), calculates the value of the next element until the last element is computed, and when there are no more elements, the Stopiteration error is thrown.

In fact, we can use the for loop instead of the next () method, which is more consistent with the efficient programming idea:

12345678910111213 >>>  gen =  ( x  for   in  range (1, 10)) >>>  for  num  in  gen: ...      print num ...  1 2 3 4 5 6 7 8 9

Generator is very powerful. If the algorithm is more complex, it can also be implemented by using a function that is not possible with a For loop that resembles a list-generated type.

For example, the famous Fibonacci sequence (Fibonacci), except for the first and second numbers, can be summed up by the top two numbers:

1 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

The Fibonacci sequence is not written in a list, but it is easy to print it out with a function:

1234567 deffib(max):     =0    a, b =01    whilen < max:         print b         a, b =b, a +b         =+1

The above function can output the number of the first n of the Fibonacci sequence:

1234567 >>> fib(6) 1 1 2 3 5 8

Looking closely, it can be seen that the FIB function is actually a calculation rule that defines the Fibonacci sequence, starting with the first element and extrapolating any subsequent elements, which are actually very similar to generator.

In other words, the above functions and generator are only a step away. To turn the FIB function into a generator, just change print B to yield B:

1234567 deffib(max):     =0    a, b =01    whilen < max:         yieldb         a, b =b, a +b         =+ 1

This is another way to define generator. If a function definition contains the yield keyword, then the function is no longer a normal function, but a generator:

12 >>> fib(6) <generator object fib at 0x104feaaa0>

The most difficult thing to understand here is that the generator is not the same as the execution flow of the function. The function is executed sequentially, the return statement is encountered, or the last line function statement is returned. The function that becomes generator, executes at each call to next (), encounters a yield statement return, and executes again from the yield statement that was last returned.

To give a simple example, define a generator and return the number 1,3,5 in turn:

12345678910111213141516171819202122 >>> def odd(): ...     print ‘step 1‘...     yield 1 ...     print ‘step 2‘...     yield 3 ...     print ‘step 3‘...     yield 5 ... >>> o = odd() >>> o.next() step 1 1 >>> o.next() step 2 3 >>> o.next() step 3 5 >>> o.next() Traceback (most recent call last):   File "<stdin>", line 1, in<module> StopIteration

As you can see, odd is not a normal function, but a generator, in the execution process, encounter yield is interrupted, the next time continue to execute. After 3 yield, no yield can be executed, so the 4th call to Next () is an error.

Returning to the FIB example, we constantly call yield during the loop, and we are constantly interrupted. Of course, you have to set a condition for the loop to exit the loop, or it will produce an infinite sequence.

Similarly, after changing a function to generator, we basically never call it with next (), but instead use the For loop to iterate:

123456789 >>> forinfib(6): ...     print n ... 1 1 2 3 5 8

Generator is a very powerful tool, in Python, you can simply change the list generation to generator, or you can implement the generator of complex logic through functions.

To understand how generator works, it is continuously calculating the next element during the for loop and ending the For loop in the appropriate condition. For the generator of a function, a return statement or execution to the last line of the function body is the end of the generator instruction, and the For loop ends with it.

In-depth understanding of the Python Builder (Generator)

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.