1.10 Generator

1. The definition of generator

Generator may be understood as a data type, that data type automatically iterative protocol (other data types need to call its own built iter method)

In Python, while circulating, while the calculated mechanism, known as a generator.

2. The effect generator

1. List the formula, we can directly create a list, however, subject to memory limitations, the list is certainly limited capacity.

2. Also, create a list containing one million elements, not only take up much storage space, if we only need to access the first few elements behind the elements that the vast majority of occupied space are wasted.

3. Therefore, if the list element can be calculated out in accordance with an algorithm that if we can continue to calculate in the process cycle in a subsequent elements?

4. This eliminates the need to create a complete list, thus saving a lot of space. In Python, while circulating mechanism for this calculation, known as a generator: generator.

3. Generator Works

1) is a function generator, it remembers the position of the function body when the last return.

2) on a second (or n-th) to jump generator function call to the middle of the function call and all local variables last remains constant.

3) not only generates "remembers" its data state; generator also "remembers" its position in the flow control configuration.

4) a function generator, and the function parameters are retained.

5) When a call to the next iteration, the parameters used are the first under the reserved, that is, when all the parameters throughout the function call is the first time invoked reserved, rather than create new of

4. yield generator operating mechanism

In Python, yield can be such that a generator.

1) When you ask the builder to a number, the generator will execute until the yield statement appeared, the generator parameters of the yield to you, then the generator will not continue to run down.

2) When you ask him to be the next number, he will begin to run from the last state until the yield statement appeared, the parameters for you, then stop. So again

3) in python, when you define a function, use the yield keyword, this is a function generator

4) It will perform and other common functions there are many different function returns an object rather than your usual, as with the return statement, the resulting value can be obtained. If you want to get the value, it must call the next () function

5) Whenever a call to the next iterator function, a function generator running to yield place, behind the return value of yield and pause in this place, all the states will be kept live until the next next function is called, or touch abnormal loop exits.


# Yield achieved fib number
def fib(max_num):
    a,b = 1,1
    while a < max_num:
        yield b
        a,b=b,a+b
 
g = fib (10) # generates a generator: [1, 2, 3, 5, 8, 13]
print (g .__ next __ ()) # first call Returns: 1
print (list (g)) # the remaining elements of the list becomes: [2, 3, 5, 8, 13]

Guess you like

Origin www.cnblogs.com/lihouqi/p/12664239.html