Home Backend Development Python Tutorial In-depth understanding of Python generators (Generator)

In-depth understanding of Python generators (Generator)

Oct 17, 2016 pm 04:22 PM

We can create a list simply and directly through list generation, but due to memory constraints, the list capacity is definitely limited. Moreover, creating a list containing 1 million elements not only takes up a lot of storage space, but if we only need to access the first few elements, the space occupied by most of the subsequent elements is wasted.

So, if the list elements can be calculated according to a certain algorithm, can we continuously calculate subsequent elements during the loop? This eliminates the need to create a complete list, saving a lot of space. In Python, this mechanism of looping and calculating at the same time is called a generator.


To create a generator, there are many ways. The first method is very simple. Just change the [] of a list generation expression to () to create a generator:

>>> mylist = [ x for x in range(1, 10)]
>>> mylist
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> gen = (x for x in range(1,10))
>>> gen
<generator object <genexpr> at 0x7f1d7fd0f5a0>
Copy after login

The difference between creating mylist and gen is only the outermost [] and ( ), mylist is a list, and gen is a generator.

We can directly print out each element of the list, but how do we print out each element of the generator?

If you want to print them out one by one, you can use the next() method of the generator:

>>> gen.next()
1
>>> gen.next()
2
>>> gen.next()
3
...
>>> gen.next()
9
>>> gen.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
Copy after login

We have said that the generator saves the algorithm. Each time next() is called, the value of the next element is calculated. , until the last element is calculated and there are no more elements, a StopIteration error is thrown.


In fact, we can use for loop instead of next() method, which is more in line with efficient programming ideas:

>>> gen = ( x for x in range(1, 10))
>>> for num in gen:
...     print num
... 
1
2
3
4
5
6
7
8
9
Copy after login


generator is very powerful. If the calculation algorithm is relatively complex and cannot be implemented using a for loop similar to list generation, you can also use a function to implement it.


For example, in the famous Fibonacci sequence, except for the first and second numbers, any number can be obtained by adding the first two numbers:

1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Copy after login

Fi The Boracchi sequence cannot be written using list generation, but it is easy to print it out using a function:

def fib(max):
    n = 0 
    a, b = 0, 1
    while n < max:
        print b
        a, b = b, a + b
        n = n + 1
Copy after login

The above function can output the first N numbers of the Fibonacci sequence:

>>> fib(6)
1
1
2
3
5
8
Copy after login

If you look closely, you can see that the fib function actually defines the calculation rules of the Fibonacci sequence. It can start from the first element and calculate any subsequent elements. This logic is actually very similar to the generator.


In other words, the above function is only one step away from the generator. To turn the fib function into a generator, just change print b to yield b:

def fib(max):
    n = 0 
    a, b = 0, 1
    while n < max:
        yield b
        a, b = b, a + b
        n = n + 1
Copy after login

This is another way to define a generator. If a function definition contains the yield keyword, then the function is no longer an ordinary function, but a generator:

>>> fib(6)
<generator object fib at 0x104feaaa0>
Copy after login

Here, the most difficult thing to understand is that the execution flow of generator and function is different. Functions are executed sequentially and return when encountering a return statement or the last line of function statements. The function that becomes a generator is executed every time next() is called, returns when encountering a yield statement, and continues execution from the yield statement returned last time when executed again.


As a simple example, define a generator that returns the numbers 1, 3, and 5 in sequence:

>>> def odd():
...     print &#39;step 1&#39;
...     yield 1
...     print &#39;step 2&#39;
...     yield 3
...     print &#39;step 3&#39;
...     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 "<stdin>", line 1, in <module>
StopIteration
Copy after login

You can see that odd is not an ordinary function, but a generator. During execution, it encounters It will be interrupted when yield is reached and execution will continue next time. After executing yield three times, there is no more yield to execute, so an error is reported when next() is called for the fourth time.


Back to the fib example, if we keep calling yield during the loop, it will continue to be interrupted. Of course, you need to set a condition for the loop to exit the loop, otherwise an infinite number will be listed.


Similarly, after changing the function to a generator, we basically never use next() to call it, but directly use a for loop to iterate:

>>> for n in fib(6):
...     print n
...
1
1
2
3
5
8
Copy after login

generator is a very powerful tool, In Python, you can simply change the list generation into a generator, or you can use functions to implement complex logic generators.

To understand the working principle of the generator, it continuously calculates the next element during the for loop and ends the for loop under appropriate conditions. For a generator changed from a function, when the return statement is encountered or the last line of the function body is executed, it is the instruction to end the generator, and the for loop ends accordingly.


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

See all articles