I. Functions of yield
Python has a very useful syntax called generator, using the keyword yield. yield's role is to turn a function into a generator (generator), playing a delayed role.
A function with a yield is called a generator in Python, which means that when you call the function, the code inside the function is not executed immediately, but the function just returns a generator (Generator Iterator).
1. Iterate over the generator using the next method
How is the generator function called? The answer is the next function.
def generator(): for i in range(10) : yield i*i gen = generator() print(gen) print("first iteration:") print(next(gen)) print("second iteration:") print(next(gen)) print("third iteration:") print(next(gen)) print("fourth iteration:") print(next(gen))
Program output.
<generator object generator at 0x7ffaad115aa0>first iteration:
0
second iteration:
1
three iteration:
4
four iteration:
9
At the first call of the function to the next(gen) function, the generator function executes from the beginning to yield and returns the value after yield.
On the second call of the function to the next(gen) function, the generator function continues from where the last yield ended until the next execution to the yield and returns the value after the yield. And so on.
2. Using the send() method to communicate with generator functions
def generator(): x = 1 while True: y = (yield x) x += y gen = generator() print("first iteration:") print(next(gen)) print("send iteration:") print((10))
Code output:
first iteration:
1
send iteration:
11
Generator (generator) function with a yield expression will be processed x sent to the generator (Generator) caller; generator (generator) caller can then send function, the external information to replace the return value of the generator's internal yield expression, and assigned to y, and participate in the subsequent iteration process.
Example 2:
def dog(name): food_list=[] print('Dog%s ready to eat'%name) while True: food=yield food_list # Pause food=yield='bucket of slop' print('Dog%s ate it%s'%(name,food)) food_list.append(food) dog_name=dog('Ami') next(dog_name) dog_name.send('Meat') dog_name.send('Bun')
Code output:
Ami the Dog is ready to eat.
Ami the dog ate meat.
Ami the dog ate the bun.
3. Benefits of Yield
The main reason why Python has to provide such a solution is memory footprint and performance considerations. Look at code similar to the one below:
def my_range(start,stop,step=1): while start<stop: yield start #Pause start += step g=my_range(1,30,2) #1 3 print('first iteration:') print(next(g)) print('scond iteration:') print(next(g)) print(next(g))
running result
first iteration
1
scond iteration
3
5
Second, summarize the function of yield
1. Provide a way to customize the iterator
2, can save memory space
3, yield can suspend the function, return value
4, yield can return value, at the same time can be assigned, as follows:
yield value
x=yield
x= yield value
III. yield VS return
Similarities: both are used within functions, both can return values, no type restrictions, no limit on the number of
Difference: return can only return a value once, yield can return multiple values
to this article on the basic use of Python Yield and Yield and return of the difference between the article is introduced to this, more related Python Yield and return of the difference between the contents of the search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!