search
HomeBackend DevelopmentPython TutorialPython-detailed explanation of generators

1. What is a generator

With list generation, we can directly create a list. However, 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 will be wasted. So, if the elements of the list can be calculated according to a certain algorithm, can we continuously calculate the 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: generator.

2. Create a generator method

Method 1

There are many ways to create a generator. The first method is very simple, just change the [ ] of a list generation to ( )

The difference between creating L and G is only the outermost [ ] and ( ), L is a list, and G is a generator. We can directly print out each element of L, but how do we print out each element of G? If you want to print them out one by one, you can get the next return value of the generator through the next() function:


Running results:

Running results:

The generator saves the algorithm, and each time next( is called G), calculate the value of the next element of G until the last element is calculated. When there are no more elements, a StopIteration exception is thrown. Of course, this kind of continuous calling next() is really abnormal. The correct method is to use a for loop, because the generator is also an iterable object. So, after we create a generator, we basically never call next(), but iterate it through a for loop, and don't need to care about the StopIteration exception.

Method 2

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

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, ...

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


Run result:

Looking carefully, you can see that the fib function is actually defined After understanding the calculation rules of the Fibonacci sequence, you 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:


Running results:

In the above example of fib, 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 generator, we basically never use next() to get the next return value, but directly use the for loop to iterate:

Running results:

#But when calling the generator using a for loop, I found that I could not get the return value of the generator's return statement. If you want to get the return value, you must capture the StopIteration error. The return value is included in the value of StopIteration:


Running result:

3.send

Example: When yield is executed, the gen function is temporarily saved and returns the value of i; temp receives the value sent by c.send("python") next time, c .next() is equivalent to c.send(None)

Use next function


Running result:

Use __next__() method


Running result:

Use send

Run result:

4. Implement multi-tasking

Simulate multi-tasking implementation method One: Coroutine


Run result:

Summary

Generator is a function that remembers the position in the function body when it last returned. The second (or nth) call to a generator function jumps to the middle of the function, leaving all local variables unchanged from the previous call.

A generator not only "remembers" the state of its data; a generator also "remembers" its position within a flow control construct (in imperative programming, this construct is not just a data value).

Features of the generator:

1. Save memory

2. When iterating to the next call, the parameters used are all retained from the first time. , that is to say, the parameters of all function calls are retained when called for the first time, rather than newly created

5. Iterator

Iteration is to access the elements of the collection a method. An iterator is an object that remembers the position of a traversal. The iterator object starts accessing from the first element of the collection until all elements have been accessed. Iterators can only go forward and not backward.

1. Iterable objects

The data types that directly act on the for loop are as follows:

The first type is collection data types, such as list, tuple, dict , set, str, etc.;

One category is generator, including generator and generator function with yield.

These objects that can be directly used in for loops are collectively called iterable objects: Iterable.

2. Determine whether iterable

You can use isinstance() to determine whether an object is an Iterable object:


Running results:

The generator can not only act on the for loop, but can also be continuously called by the next() function and return the next value, until finally a StopIteration error is thrown to indicate that it cannot Continue to return to the next value.

3. Iterator

An object that can be called by the next() function and continuously returns the next value is called an iterator: Iterator.


Running result:

4.iter() function

Generator They are all Iterator objects, but although list, dict, and str are Iterable, they are not Iterators.

To convert list, dict, str and other Iterables into Iterator, you can use the iter() function:


Running result:

Summary

·All objects that can be used in the for loop are of type Iterable;

·All objects that can be used in the next() function are of type Iterator

·Collection data types such as list, dict, str, etc. are Iterable but not Iterator, but you can obtain an Iterator object through the iter() function.

·The purpose is to reduce the occupied content when using collections.

6. Closure

1. Function reference


Running result:

Illustration:

2. What is closure


Run result:

3. Look at a practical example of closure:


Run result:

In this example, the function line and variables a and b form a closure. When creating the closure, we specify the values ​​of these two variables through the parameters a and b of line_conf. In this way, we determine the final form of the function (y = x + 1 and y = 4x + 5). We only need to transform the parameters a and b to obtain different straight line expression functions. From this, we can see that closures also have the effect of improving code reusability.

If there is no closure, we need to specify a, b, x every time we create a straight line function. In this way, we need to pass more parameters, which also reduces the portability of the code. If you encounter any problems during the learning process or want to obtain learning resources, you are welcome to join the learning exchange group

626062078, we Learn Python together!

The above is the detailed content of Python-detailed explanation of generators. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.