Python keyword yield

1. Introduction to yield

Yeild is a keyword of python. If you want to understand the role of yield, you must understand generators. Before understanding generators, you need to understand what iterables are.

2. Iterable objects

When you create a list, you can traverse the list, read each element of it, and the process of reading the list elements one by one is called iteration.

mylist = [2,3,4,5]
for i in mylist:
  print(i)

Objects that can be iterated using the for...in... syntax area are all iterable objects. Like dictionaries, lists, arrays, and strings are all objects that can be iterated.

Three. Generator

The generator is an iterator (iteratos), but it can only be iterated once. The generator does not store all the values ​​in memory, but generates these values ​​in real time:

mygenerators = (i*i for i in range(3))
for i in mygenerators:
  print(i)

The [] of the column comprehension is changed to (), and nothing else is changed. But mygenerators is no longer a list but a generator. After the generator iterates once, it cannot iterate again. Calculate 0, and then continue to calculate 1 without saving the result and state. It is best to calculate 4 and generate them one by one.
This is just one way to create a generator, another way is the yield keyword of our protagonist today.

def create_generator():
   for i in range(3):
     yield i*i
 mygenerator = create_generator()
 for i in mygenerator:
   print(i)

Yield is a keyword similar to return. When we call this function, it does not return the result of the calculation. Instead, it returns a generator. The result is calculated only when the generator is iterated.
for i in mygenerator: the first time the function is executed to the yield keyword position, the value of ii is returned, and then the function is suspended (the state of the function execution is saved). for i in mygenerator: In the second loop, it will continue to execute the function just now (the suspended position), that is, execute the for loop in the generator, return the value of ii, and then suspend the function again. It ends until there is no value in the generator to return.
Yield can return a value, but it will not end the execution of the function. If there is code behind the function, it can also be executed.

def test():
  for i in range(3):
    yield i
    print(‘我是生成器’)
mygen = test()

for i in mygen:
  print(i)

0
我是生成器
1
我是生成器
2
我是生成器

The role of yield

It is necessary to create a very large list and directly using the list comprehension may cause memory to be exhausted. This code cannot create the list, and the computer memory is insufficient to save the list.
[i for i in range(1000000000)]
But you can use the generator to create it successfully, and then take it out of the generator when you need it.

Five. The benefits of yield

1. Instead of taking out all the data and storing it in the memory, it returns an object. You can get the data through the object, and you can save the memory space.
2. In addition to returning a value, it will not terminate the cycle.
The efficiency of using yield is higher than that of ordinary lists.

def test():
  for i in range(1,111000000):
   if i%2 ==0:
    yield i
def test1():
  result =[]
  for i in range(1,111000000):
    if i%2 ==0:
    	result.append(i)
    	return result

0.8925411202393 #Generator writing time-consuming
1.14441919322678223 #General writing time-consuming

Guess you like

Origin blog.csdn.net/weixin_44726976/article/details/109058763