Understanding and summarizing of Python generators

Source: Internet
Author: User

1. Generator

Using iterators, we can generate the data in each iteration (through the next () method) and follow a specific pattern. But when we implement an iterator, we need to record the current iteration to the state we want to generate the next data based on the current state. To achieve a record of the current state and iterate with the next () function, we can use a simpler syntax, the generator (generator). A generator is a special kind of iterator.

2. Create a builder Method 1

There are a number of ways to create a generator. The first method is simple, just change the [] to () of a list-generated

3. Create a builder Method 2

Generator is very powerful. If the algorithm is more complex, it can also be implemented by using a function that is not possible with a For loop that resembles a list-generated type.

In the way the generator is implemented, we implement the basic logic that was originally implemented in the iterator __next__ method into a function, but instead of returning the return value of each iteration to yield, the newly defined function is no longer a function, but a generator. Simply put: As long as you have the yield keyword in def, it's called the generator.

Instead of executing the function body by using the generator in the way that the function is called, the generator object is returned, and the generator can be used in a way that uses iterators.

Summarize
A function that uses the yield keyword is no longer a function, but a generator. (a function that uses yield is the generator)
The yield keyword has a two-point effect:
Saves the current running state (breakpoint), and then pauses execution, pending the generator (function)
Returns the value of the expression after the yield keyword as a return value, which can be understood as the function of return
You can use the next () function to let the generator continue execution from the breakpoint, which is the wake-up generator (function)
The generator in Python3 can return the final running return value using return, while the generator in Python2 does not allow return returns a return value (that is, you can use return to exit from the generator, but you cannot have any expressions after return).

4. Use Send to wake up
In addition to using the next () function to wake the generator to continue execution, we can also use the Send () function to wake execution. One advantage of using the Send () function is that you can pass in an additional data to the breakpoint while waking.

Example: When executing to yield, the function of the Gen function is temporarily saved, returning the value of I; Temp receives the next c.send ("Python"), send the value sent over, C.next () equivalent C.send (None)

In [ten]: Def gen ():
.....: i = 0
.....: While I<5:
....: temp = yield I
....: Print (temp)
.....: i+=1
....:

Understanding and summarizing of Python generators

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.