python–generator generator_python code automatic generator

Article directory

  • Preface
    • 1. What is a generator?
    • 2. The difference between generator functions and ordinary functions
    • 3.The following is an example of a simple generator function
    • 4. Generator function execution logic
    • 5.next()
    • 6.Function
    • 7. Generator function syntactic sugar-yield from
  • Introduction to zero-based Python learning resources
    • Summary of Python learning route
    • Must-have development tools for Python
    • Collection of 600 Python learning videos
    • Actual cases
    • 100 Python exercises
    • Interview questions
  • Data collection

Foreword

1. What is a generator?

In Python, a generator is a special iterator that is implemented through functions. Generator functions can pause and resume execution during execution, and can dynamically generate a series of values without generating all values at once, thus saving memory space and computing resources.

  • Can be obtained from generator expressions.

  • You can use the yield keyword to get a generator function, and then call this function to get a generator object. When the generator function containing the yield statement is called to generate a generator object, the function body of the generator function will not be executed immediately.

  • Generators (objects) can use the next method. next(generator) will execute backward from the current position of the function to the first yield statement encountered later. It will interrupt function execution and return the value of the yield expression. Call next again. The function resumes execution from the last interruption point and continues to call the next function. If the generator function ends execution (return statement is called explicitly or implicitly), a StopIteration exception will be thrown.

  • The next() method will directly report an error if it exceeds the limit. If there are no special needs, it is generally recommended to use a for loop to traverse.

  • The generator object can only be traversed one time at a time and cannot be read back.

2. The difference between generator functions and ordinary functions

The difference between a generator function and an ordinary function is that the yield statement is used in the generator function to generate a value instead of the return statement to return a value. When a generator function is called, it returns a generator object instead of directly executing the code in the function body. When the generator object is iterated, the code in the generator function will be executed line by line until it encounters the yield statement, at which time execution will be suspended and the value after yield will be returned as the next value of the iterator. On the next iteration, the generator function will continue execution from the code after the yield statement until it encounters the yield statement again, and so on.

3. The following is an example of a simple generator function

It generates a sequence of natural numbers starting from 0:

return

numbers = natural_numbers()

Isn’t it so difficult that numbers should not be equal to the return value None of the function natural_numbers?

Although there is an explicit return statement (there may be no return), when python compilation is executed to the yield statement, the function natural_numbers will be labeled with a generator, so calling the generator function natural_numbers can return a generator object and save it to numbers.

def natural_numbers():
    n = 0
    while True:
        yield n
        n + = 1
    return
numbers = natural_numbers()

Obtain the values in the sequence sequentially through the iterator-next() method:

Neither yield nor return is the return value of the generator function. Only when I use the next function on the generator object will the generator function body start running.

print(next(numbers)) # Output: 0
print(next(numbers)) # Output: 1
print(next(numbers)) # Output: 2

Since the generator object is an iterator, it can be used in a for loop, for example:

for n in natural_numbers():
    if n > 10:
        break
    print(n)

4. Generator function execution logic

  • There can be multiple yield statements in the same generator function.
for i in range(5):
    yield i #The yield statement is executed 5 times.
  • When traversing the generator function object, the execution of the function will be interrupted when it reaches the yield statement, and the value of the yield expression will be returned.
  • When the object is traversed again, the function continues execution from the previous yield breakpoint position and executes to the next yield statement.
  • Repeat the previous step until the function ends normally or a return statement is encountered.
  • The return statement can still terminate the function, but the return value of the return statement cannot be obtained.
  • return will cause the current function to return and execution cannot continue. The generator object has no iterable depth at this point.
  • Continue to use the next() method and throw a StopIteration exception.
  • If the function does not have an explicit return statement, and if the generator function is executed to the end (equivalent to executing return None), a StopIteration exception will also be thrown.
 def gen():
        print('line 1')
        yield 1
        print('line 2')
        yield 2
        print('line 3')
        return 3
        yield 4

5.next()

  • If you directly use the next() method to call the generator function itself, then a generator object will be regenerated each time, and it will not be possible to continuously traverse a certain generator object downwards.
 next(gen()) # line 1
    next(gen()) # line 1
    
    ```
    ```
    line 1
    1
    line 1
    1   
    
  • Therefore, a variable needs to be used to receive the generator object returned by gen().
 g = gen()
    print(g)
    
    ```
    ```
    <generator object gen at 0x000001C19302C228>
    
  • Use the next method to traverse the generator object one by one and output normally.
 print(next(g)) # line 1
    print(next(g)) # line 2
    
 line 1
   1
   line 2
   2
   
  • But if you continue to use next() to traverse, the interpreter will report an error, StopIteration, indicating that the traversal has exceeded the limit. The function has returned at return 3 and will not be executed until the subsequent yield.
 print(next(g)) # StopIteration
    
 line 3
   
   -------------------------------------------------- -----------------------
   
       StopIteration Traceback (most recent call last)
       <ipython-input-91-9757af9844f9> in <module>
       ----> 1 print(next(g)) # StopIteration
       
       StopIteration: 3
       
       StopIteration: 3
   

6. Function

  • Generators can be used to process large data sets, reducing memory usage. In addition, generators can be used for lazy calculations, which only calculate when needed, thus improving the efficiency of the program.
  • The generator function can be used as an interval to interrupt the execution of the loop body. Each time the generator object is called, the function loop body will continue to run once.
  • Infinite loop:
def counter():
i = 0
while True:
i+=1
yield i
def inc(c):
return next(c)
c = counter()
print(inc(c))

  • counter:
def inc():
def counter():
i = 0
while True:
i+=1
yield i
c = counter()
return lambda : next(c)
foo = inc()
print(foo()) # 1
print(foo()) # 2
print(foo()) # 3

  • Dealing with recursion problems: The original infinite loop recursion will cause the interpreter to report an error when the maximum recursion depth is exceeded. However, after inserting the yield statement, the loop body is stuck, and the function loop body will be executed only once when the generator object is called.
def fib():
x = 0
y=1
while True:
yield y
x, y = y, x + y
foo = fiber()
for _ in range(5):
print(next(foo))

7. Generator function syntax sugar-yield from

  • Yield from is a new syntax that appeared in Python 3.3

  • yield from iterable is syntactic sugar of the form for item in iterable: yield item

def inc():
for x in range(1000):
yield x

def inc():
yield from range(1000) ## Syntax sugar for interpreter optimization


-END-

Reader benefits: If you are interested in Python, this set of python learning materials will definitely be useful to you

For beginners with zero basic knowledge:

If you are a novice and want to get started with Python quickly, you can consider it.

On the one hand, the learning time is relatively short and the learning content is more comprehensive and focused.
The second aspect is that you can plan your study plan and direction based on these materials.

Includes: Python activation code + installation package, Python web development, Python crawler, Python data analysis, artificial intelligence, machine learning, Python quantitative trading and other learning tutorials. Let you learn Python systematically from scratch!

Introduction to zero-based Python learning resources

Learning roadmap for all directions of Python, knowing what to learn in each direction

② More than 600 Python course videos, covering essential basics, crawlers and data analysis

③ More than 100 Python practical cases, including detailed explanations of 50 very large projects, learning is no longer just about theory

④ 20 mainstream mobile games forced release Retrograde forced release tutorial package for crawler mobile games

Crawler and anti-crawler attack and defense tutorial package, including 15 large-scale website compulsions

Practical reverse engineering of crawler APP Tutorial package, including detailed explanations of 45 top-secret technologies

⑦ Over 300 good Python e-books, ranging from entry-level to advanced

⑧ Exclusive Python comic tutorial produced by Huawei, you can also learn it on your mobile phone

Real Python interview questions from Internet companies over the years, very convenient for review

Summary of Python learning routes

The technical points in all directions of Python are organized to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively. (Get the full set of tutorials at the end of the article)

Must-have development tools for Python

Warm reminder: The space is limited, the folder has been packaged, and the access method is at: end of the article

Collection of 600 Python learning videos

Watch zero-based learning videos. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher’s ideas in the video, from basic to in-depth.

Actual cases

Optical theory is useless. You must learn to follow along and practice it in order to apply what you have learned to practice. At this time, you can learn from some practical cases.

100 Python exercises

Check learning results.

Interview questions

Data collection

The above-mentioned complete version of the complete set of Python learning materials has been uploaded to the CSDN official. If you need it, friends can scan the CSDN official certification QR code below on WeChat to get it↓↓↓

syntaxbug.com © 2021 All Rights Reserved.