SoFunction
Updated on 2025-03-02

A detailed analysis of yield keywords in Python

Preface

There is a very useful syntax in python called generator, and the keyword used is yield. Effectively utilizing the generator tool can effectively save system resources and avoid unnecessary memory usage.

A piece of code

def fun():
for i in range(20):
 x=yield i
 print('good',x)
if __name__ == '__main__':
a=fun()
a.__next__()
x=(5)
print(x)

This code is short, but it interprets the core usage of the yield keyword, namely, generate one by one. Here are the values ​​generated by the two generators, namely 0 and 1. We will explain the difference between the next function and the send() function respectively.

Regarding the __next__ function, let me first explain it here. We can use the __next__() function to continuously obtain the number that meets the rules of the fun function until the end of 19. This code looks like this:

def fun():
for i in range(20):
 x=yield i
if __name__ == '__main__':
for x in fun():
 print(x)

The effect of this code is exactly the same as the following code

if __name__ == '__main__':
for i in range(20):
 x=yield i

Calling the generator for..in is considered the basic usage of the generator, but it is not very meaningful to just use for..in. The most important functions in the generator are the two functions sent and __next__. The following is a detailed explanation of these two functions.

sent function

This is particularly emphasized in the sent function because the sent function is not that intuitive. The __next__ function is easy to understand, which starts from the previous terminating point and ends at the next yield. The return value is the value of the yield expression.

For example in the initial code:

def fun():
for i in range(20):
 x=yield i
 print('good',x)

When we call the __next__ function for the first time, we start from the starting point of fun and then end at yield. It should be noted that the assignment statement will not be called. Here, yield i and the meaning are similar to return.

However, when the __next__ function is called the second time, it will start directly from the end of the previous yield, that is, first execute the assignment statement, then output the string, enter the next loop, until the next yield or generator ends

Looking at the initial code again, we can find that when the second call was called, we did not choose to use the __next__ function, but used a sent() function. It should be noted here that the usage of the sent() function is different from the __next__ function. The sent() function can only start after yield and end at the next yield. This means that the first call must use the __next__ function.

The most important function of the sent() function is that it can assign values ​​to the assignment statement corresponding to yield, such as in the above code

x=yield i

If the __next()__ function is called, then x=None. But if sent(5) is called, then x=5. Except for the two features listed above, there is no difference between sent and next. The sent function will also return the value corresponding to the yield expression.

Next function calls may be limited

It is important to note that although it is a generator. But the number of calls to next function may be limited. For example, the following code

def fun():
for i in range(20):
 x=yield i
 print('good',x)
if __name__ == '__main__':
a=fun()
for i in range(30):
 x=a.__next__()
 print(x)

The function in the generator only loops 20 times, but the next function is called 30 times, and the StopIteration exception will be triggered.

Summarize

The above is the yield keyword in Python introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!