“This is the 28th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

There are two useful types of functional iterators and generators in Python. When we first started, it was easy to think of iteration as just dealing with the elements of a list, set, or other sequence. However, that’s not what they do.

When we encounter a list [1,2,3,4,5,6] that needs to be traversed, we can use the following method:

  • The for loop iterates over the print
  • Next () method prints

We can also create personalized iterator objects, construct generator functions, and so on.

The iterator protocol in Python mainly uses two magic methods: __iter__() and __next__().

  • __iter__()Method to create a__next__()Method
  • __next__()Method returns the next iterator object

Generators in Python that have only iterative operations are also iterators. In Python, we can use the yield function to implement generators.

In this issue, we will introduce the principles and usage of generators in detail, Let’s go~~~

1. What is yield?

First we need to recognize a generator, which literally means the operation of circular computation. In Python, provide a mechanism for looping through calculations at the same time.

Generators are designed to solve the problem of memory consumption when using sequences to store large amounts of data. We can store data according to certain rules, calculate as an algorithm, in the loop process through calculation, so as not to create a complete sequence, thus greatly saving space.

Yield is one of the methods that implements generators. When a function uses yield, the function becomes a generator. By calling this function, you create a generator object.

2. The principle of yield

A generator that calls the next() method repeatedly, mainly through a loop, until an exception is caught.

A function with a yield method is also a generator, creating the following chestnut:

def test(n) :

    print("starting...")

    while True:

        res = yield n

        print ("res:",res)


g = test(5)

print(next(g))

print("# # # # # #")

print(next(g))
Copy the code

We can see the printed log output:

  1. The test function has the yield keyword, indicating that it is a generator and will not be executed
  2. When the test function encounters the next() method, the test internal step is executed
  3. The program aborts until it encounters the yield keyword
  4. Until the next next() method wakes up, perform the yield follow-up step and print() method. At this point, res is not assigned.
  5. Enter the inner step of the while loop again, and similarly abort the loop when the yield keyword is encountered

When we need to assign a value to an element in the generator, we call the.send() method

Therefore, a function with yield performs the following internal operations:

  • Yield method: Equivalent to Return, the program encounters yield and immediately aborts subsequent steps
  • When the generator is called again, the next() method wakes up and continues with yield subsequent steps
  • You can also call the send() method, which you can wake up and pass in a value to continue with the yield subsequent steps

Generators are iterable and can only be read once at a time. This is often used in combination with a for loop.

3. yield

Instead of a sequence containing a large amount of data, we can often use functions with yields to create generators.

import sys

def test(n) :

    print("start")

    while n > 0:

        yield n
        n-=1

    print("end")


a = [x for x in range(1000)]

b= test(1000)

print("A Memory size:",sys.getsizeof(a))
print("B Memory size:",sys.getsizeof(b))
Copy the code

conclusion

In this installment, we’ll focus on the details of generators and the yield keyword. Generators are one of the iterators and are used only for iterative operations. Internally, the generator calls either the next() or send() methods to access the next iterator object.

We can implement generators by defining functions with the yield keyword. Yield is equivalent to return, but can support passing arguments.

That’s the content of this episode, please give your thumbs up and comments. See you next time