Python Builder (Generator) detailed

Source: Internet
Author: User
With list generation, we can create a list directly. However, with memory limitations, the list capacity 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, 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).

Simple 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:
Copy the Code code as follows:


>>> L = [x * x for x in range (10)]
>>> L
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> g = (x * x for x in range (10))
>>> g
At 0x104feab40>


The difference between creating L and G is only the outermost [] and (), L is a list, and G is a 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:
Copy the Code code as follows:


>>> G.next ()
0
>>> G.next ()
1
>>> G.next ()
4
>>> G.next ()
9
>>> G.next ()
16
>>> G.next ()
25
>>> G.next ()
36
>>> G.next ()
49
>>> G.next ()
64
>>> G.next ()
81
>>> G.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.

Of course, this constant invocation of the next () method is so perverted that the correct approach is to use a For loop because generator is also an iterative object:
Copy the Code code as follows:


>>> g = (x * x for x in range (10))
>>> for N in G:
... print N
...
0
1
4
9
16
25
36
49
64
81


So, after we create a generator, we basically never call the next () method, but instead iterate over it with a for loop.

Generator with yield statement

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:
Copy the Code code as follows:


def fib (max):
N, a, b = 0, 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:
Copy CodeThe code is as follows:


>>> 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:
Copy the Code code as follows:


>>> 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):
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:
Copy the Code code as follows:


>>> for N in fib (6):
... print N
...
1
1
2
3
5
8


Enhanced Generators

In python2.5, some of the reinforcing features are added to the generator, so in addition to next () to get the next generated value, the user can return the value to the generator [Send ()], throw an exception in the generator, and require the generator to exit [Close ()]
Copy the Code code as follows:


Def gen (x):
Count = X
While True:
val = (yield count)
If Val is not None:
Count = Val
Else
Count + = 1

f = Gen (5)
Print F.next ()
Print F.next ()
Print F.next ()
print ' ==================== '
Print F.send (9) #发送数字9给生成器
Print F.next ()
Print F.next ()


Output
Copy CodeThe code is as follows:


5
6
7
====================
9
10
11
  • 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.