How to Implement Python Generator

Source: Internet
Author: User
Keywords python generator python generator implement
The function of generator and iterator is very similar, it also provides __next__() method, which means that the program can also call the built-in next() function to get the next value of the generator, or use a for loop to traverse the generation Device.

The difference between a generator and an iterator is that an iterator usually defines an iterator class and then creates an iterator by creating an instance; a generator first defines a function that contains a yield statement and then creates it by calling the function Builder.

Generators are a very good syntax, and Python uses generators to make programs very elegant.

Create a generator

Creating a generator requires two steps:

Define a function that contains the yield statement.

Call the function created in step 1 to get the generator.

The following program uses a generator to define a sequence of increasing differences. The program first defines a function that contains the yield statement:

def test(val, step):

    print("--------Function start execution------")

    cur = 0

    # Traverse 0~val

    for i in range(val):

        # curAdd i*step

        cur += i * step

        yield cur

Generator method

After the generator is running, developers can also provide values for the generator, in this way the generator and the "external program" dynamically exchange data.

In order to realize the dynamic exchange of data between the generator and the "external program", you need to use the send() method of the generator. The function of this method is very similar to the function of the next() function used in the previous example. They are used to obtain The next value generated by the generator, and "freeze" the generator at the yield statement; but the send() method can receive a parameter, and the parameter value is sent to the generator function.

Inside the generator function, the program can obtain the value sent by the send() method through the yield expression, which means that the program should use a variable to receive the value of the yield statement at this time. If the program still uses the next() function to get the next value generated by the generator, the yield statement returns None.

The detailed description above is summarized in two sentences:


The external program sends data through the send() method.

The generator function uses the yield statement to receive the receipt.
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.