A brief analysis of the role and usage of yield keywords in Python

  • 2020-05-17 05:48:44
  • OfStack

preface

To understand what yield is, first understand the generator ( generator ), let's talk about iterators ( iterator ), when creating a list ( list ), you can read each item one by one, which is called iteration ( iteration ).


>>> mylist = [1, 2, 3]
>>> for i in mylist :
... print(i)
1
2
3

mylist Is an iterable object. When a list is created using a list generator, an iterable object is created:


>>> mylist = [x*x for x in range(3)]
>>> for i in mylist :
... print(i)
0
1
4

You can use" for・・・ in ・・・ "To operate the iterable object, such as: list , string , files These iteration objects are very convenient for us to use, because you can repeatedly read as you wish. But you have to pre-store all the elements in memory, and when there are many elements in those objects, not every item will be useful to you.

The generator is also an iterable object, but you can only read it once, because it does not store all the values in memory. It dynamically generates the values:


>>> mygenerator = (x*x for x in range(3))
>>> for i in mygenerator :
... print(i)
0
1
4

It doesn't look any different except to replace [] with (). However, you may not use it again for i in mygenerator , because the generator can only be iterated once: first calculate 0, then continue to calculate 1, then calculate 4,1 and 1...

iterator0 It's one of those return , except that this function returns a generator.


>>> def createGenerator() :
... mylist = range(3)
... for i in mylist :
...  yield i*i
...
>>> mygenerator = createGenerator() # create a generator
>>> print(mygenerator) # mygenerator is an object!
<generator object createGenerator at 0xb7555c34>
>>> for i in mygenerator:
...  print(i)
0
1
4

This example doesn't make much sense on its own, but it makes it clear that the function will return a set of values that can only be read once. To master yield, the first thing to understand is that when you call the generator function, as in the example above createGenerator() Instead of executing the code inside the function, the program simply returns the generator object in a subtle way. The code in the body of the function does not run until each iteration of the loop (for) generator.

In the first iteration your function will execute, from the beginning to the end iterator0 Keyword, and then return iterator0 Then, each time the function is executed, it continues to execute the next loop you defined inside the function, returning that value until there is nothing left to return.

If the generator is not defined internally iterator0 Keyword, then the generator is considered empty. This may be because the loop is lost or the if/else condition is not met.

conclusion

The above is the whole content of this article, I hope the content of this article can help you to learn or use python, if you have any questions, you can leave a message to communicate.


Related articles: