Python Generator (Generator)

Source: Internet
Author: User
This article mainly introduces the details of the Python Generator (Generator). This article describes what generators are, simple generators, generators with yield statements, and enhanced generators, for more information, see generate a list. However, due to memory restrictions, the list capacity must be limited. In addition, creating a list containing 1 million elements not only occupies a large storage space, but if we only need to access the first few elements, the space occupied by the vast majority of elements is wasted.

Therefore, if the list elements can be calculated by some algorithm, can we continue to calculate the subsequent elements in the loop process? In this way, you do not need to create a complete list to save a lot of space. In Python, this type of computing mechanism is called a Generator ).

Simple generator

There are many ways to create a generator. The first method is very simple. you only need to change [] of a list generator type to (), and a generator is created:

The code is 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 0x0000feab40>


The difference between L and g is that the [] and () of the outermost layer, L is a list, and g is a generator.
We can print every element of the list directly, but how can we print every element of generator?

To print them one by one, you can use the next () method of generator:

The code is 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 call last ):
File" ", Line 1, in
StopIteration


As we have mentioned, generator stores algorithms. every time next () is called, the value of the next element is calculated until the last element is computed and no more elements exist, throw the StopIteration error.

Of course, the above constant call to the next () method is too abnormal. the correct method is to use the for loop, because the generator is also an object that can be iterated:

The code is 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


Therefore, after we create a generator, we will never call the next () method, but iterate it through the for loop.

Generator with yield statement

After careful observation, we can see that the fib function actually defines the calculation rule of the Fibonacci series. it can start from the first element and calculate any subsequent elements. this logic is very similar to generator.

That is to say, the above functions and generator are only one step away. To change the fib function to generator, you only need to change print B to yield B:

The code is 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 method for defining generator. If a function definition contains the yield keyword, this function is no longer a common function, but a generator:

The code is as follows:


>>> Fib (6)


The most difficult thing to understand here is that the execution process of generator and function is different. The function is executed sequentially. if a return statement or the last row of the function statement is returned. The generator function is executed every time next () is called. when the yield statement is returned, the yield statement returned last time is executed again.

For example, define a generator and return numbers 1, 3, and 5 in sequence:

The code is 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 call last ):
File" ", Line 1, in
StopIteration


We can see that odd is not a common function, but a generator. during the execution process, yield is interrupted and the next execution will continue. After three times of yield execution, no yield can be executed. Therefore, if you call next () for the first time, an error is returned.

Back to the fib example, we call yield continuously in the loop process, and it will be interrupted. Of course, you must set a condition for the loop to exit the loop. Otherwise, an infinite number of columns will be generated.

Similarly, after the function is changed to generator, we basically never use next () to call it, but directly use the for loop for iteration:

The code is as follows:


>>> For n in fib (6 ):
... Print n
...
1
1
2
3
5
8


Enhanced generator

In python2.5, some enhancement features are added to the generator. in addition to next (), you can return the value to the generator [send ()], throws an exception in the generator and requires the generator to exit [close ()].

The code is 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) # send number 9 to the generator
Print f. next ()
Print f. next ()


Output

The code is as follows:


5
6
7
================================
9
10
11

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.