Background
Before the concept of the Python process, the two days and a colleague chat to the process, the dead and alive can not think of what has been seen, remember a yield, the concept is unclear;
So I want to smooth out the relevant things, this article as a record of learning.
Generator
Generator (generator) is an algorithm that can be understood as a special function that has iterative ( 可迭代的对象都有一个__next()__成员方法 ) properties
Can be used as the iterative behavior of the control loop, so as to calculate on one side of the loop, which is only generated when the call is made, and can not occupy the resources of the system much.
The generator in our daily work is probably the range function in python3.x, and let's look at the difference between it and the range in python2.x:
# python2>>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> type(range(10))<type 'list'># python3>>> range(10) range(0, 10)>>> type(range(10))<class 'range'>>>> list(range(10))[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
As an example, Python2 is a list of directly generated lists, whereas calling range (10) in Python3 actually generates a range class that requires a conversion to generate a list.
Python2 the mechanism for generating lists directly, the capacity may be limited by memory in actual use. And if you create a list of 1 million elements, and we just need to access the first few elements, the vast majority of the memory footprint will be wasted. That's why PYTHON3 developers are going to be so big on this little function.
Use generator to create generator:
List-generated
There are many ways to create a generator method, and the most straightforward and straightforward way is to use a list-generation :
>>> L = [x * 2 for x in range(5)]>>> L[0, 2, 4, 6, 8]>>> G = (x * 2 for x in range(5))>>> G<generator object <genexpr> at 0x000000000309DD58>
As in the above example, if you change a list-generated form [] () , you create a generator
We can passnext()函数获得generator的返回值
>>> next(G)0>>> next(G)2>>> next(G)4>>> next(G)6>>> next(G)8>>> next(G)Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> next(G)StopIteration
Each time you call next (g), the value of the next element of G is computed until the last element is calculated, and when there are no more elements, the Stopiteration error is thrown.
Of course, this is just a test, the correct way to use is to use a for loop, 在for循环中,会自动遵循迭代规则 each call to the next () function, and do not need to care about stopiteration error.
>>> for i in G: print(i)02468
Create generator: Direct definition
Generator Functions
# 普通函数def commom_func(max): print("create counter") counter = 0 while counter < max: print(counter) print('counter increase') counter += 1# 生成器函数def yield_func(max): print("create counter") counter = 0 while counter < max: yield counter print('counter increase') counter += 1 # 生成器函数调用if __name__ == '__main__': num = yield_func(5) print(next(num)) print(next(num)) print(next(num))
---# 生成器函数调用输出create counter0counter increase1counter increase2
The following points can be seen from the above example:
- In the Yield_func function, the keyword yield, which returns a generator (as can be seen from the first line of output), is used to produce a continuous n value, and the generator produces only one result value at a time.
- When creating an instance of a generator, you only need to call it like a normal function, but this call does not execute the function.
next()函数将生成器对象作为自己的参数, at the time of the first call, he executed the YIELD_FUNC function to the yield statement, returning the resulting value 0
- We call the next () function repeatedly, and each time he executes from the last place where it was suspended until he encounters the yield keyword again
这就是定义generator的另一种方法。如果一个函数定义中包含yield关键字,那么这个函数就不再是一个普通函数,而是一个generator
Here's a more intuitive example.
def step_test(): print('step 1') yield 1 print('step 2') yield 2 print('step 3') yield 3
Call the generator, and 首先要生成一个generator对象 then use the next () function to continuously get the next return value:
>>> test = step_test()>>> next(test)step 11>>> next(test)step 22>>> next(test)step 33
Summarize
- Generator is a very powerful tool, in Python, you can simply change the list generation to generator
- 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.
- The normal function call returns the result directly, and the "call" of the generator function actually returns a generator object:
>>> step_test()<generator object step_test at 0x0000000001DE5B48>
Learn the yield and generator of python in a comprehensible language