Deep understanding of Python Builder (Generator)

Source: Internet
Author: User
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:

>>> mylist = [x for x in range (1, ten)]>>> mylist[1, 2, 3, 4, 5, 6, 7, 8, 9]>>> gen = (x for x In range (1,10)) >>> Gen
 
  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:

>>> gen.next () 1>>> gen.next () 2>>> gen.next () 3...>>> gen.next () 9>>> Gen.next () Traceback (most recent):  File "
 
  
   
  ", line 1, in 
  
   
    
   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:

>>> Gen = (x for x in range (1, ten)) >>> for Num in gen: ...     Print num ... 123456789

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, 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:

def fib (max):    n = 0     A, b = 0, 1 while    n < max:        print B        A, B = B, a + b        n = n + 1

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

>>> FIB (6)

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:

def fib (max):    n = 0     A, b = 0, 1 while    n < max:        yield b        A, B = B, a + b        n = n + 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:

>>> FIB (6)
 
  

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:

>>> def Odd ():     ... print ' Step 1 '     ... Yield 1     ... print ' Step 2 '     ... Yield 3     ... print ' Step 3 '     ... Yield 5...>>> o = odd () >>> o.next () Step 11>>> o.next () Step 23>>> o.next () step 35> >> O.next () Traceback (most recent):  File "
 
  
   
  ", line 1, in 
  
   
    
   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:

>>> for N in fib (6):     ... Print N ...

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.

  • 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.