In the following article, let's look at what the builder in Python is. Find out
What is a python generatorAnd
GeneratorIn
pythonWhat can be played in programming
function。
What is a python generator?
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.
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:
>>> L = [x * x for x in range]]>>> l[0, 1, 4, 9, N, F, $, $, 81]>>> g = (x * x for X In range (+) >>> G<generator object <genexpr> at 0x1022ef630>
After we have created a generator, we iterate over it with a for loop and do not need to care about the stopiteration error.
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:
def fib (max): N, a, b = 0, 0, 1 while n < max: print (b) A, B = B, a + b n = n + 1 return ' done '
Note that the assignment statement:
A, B = B, A + b
Equivalent:
t = (b, A + b) # T is a Tuplea = T[0]b = T[1]
However, you do not have to explicitly write out a temporary variable T to assign a value.
The above function can output the number of the first n of the Fibonacci sequence:
>>> fib (6) 112358 ' Done '
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, simply change print (b) to yield B:
def fib (max): N, a, b = 0, 0, 1 while n < max: yield b A, B = B, a + b n = n + 1 return ' done '
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:
>>> f = fib (6) >>> F<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.