Updated April 15, 2023
Introduction to Python yield Statement
In Python, yield is the keyword that works similarly as the return statement does in any program by returning the function’s values. As in any programming language, if we execute a function and it needs to perform some task and give its result to return these results, we use the return statement. The return statement only returns the value from the function, but yield statements also return multiple values by returning one value and wait, then it again resumes by saving that local state. Such functions that use yield statements instead of return statements are known as generator functions. These generator functions can have one or more yield statements.
Working of yield Statement
In Python, like other programming languages, the function uses the return statement to return the result of the function. The yield statement stops the execution of the function and resumes by returning the values one by one from the generator function called.
def function_name: statement (s) yield statement (s)
Examples of Python yield
Let’s take a simple and easy example to understand the yield statement:
def yield_function(): yield 10 yield 20 yield 30 for y in yield_function(): print(y)
From the above example, we can see the yield_function(), which wants to return more than one value, so in this case, return statement cannot be used, but to do so, we can use yield statement to print or return more than one value from the function. So yield statements are usually used on the functions that are called as generator function because the yield statement is used when we want to iterate over a sequence of values to be returned by the single function, but you do not want to save all these values in memory which means as a how the yield statement generates value to be returned each time the memory is overwritten as to it iterates and returns all the value without using memory to all the values which yield statement returns.
To print iterable values, we use for loop in normal functions. The generator function is also like a normal function, but if we use yield statements, then it generator function needs to print the iterable values returned by the functions.
def yield_func(): n = range(3) for i in n: yield i*i gen = yield_func() print(gen) for i in gen: print(i)
In the above example, we can see the function is returning more than one value in the above code it generates the generator object, so when first for loop calls the object from the function, the code in the function runs until it reaches yield statement and then the function first returns the first value, then again the for loop calls the function one more time, and it returns the second value and so on until there is no value is found to return it keeps on iterating and returning the values.
Now let us see an example with the code that demonstrates generating generator objects and printing the return values using a loop.
def gen_func(): x = 0 while x < 5: yield x x += 1 print("Simple function call without using loop:\n") print(gen_func()) print("\n") print("Below is with using a loop:") for y in gen_func(): print(y)
In this above code, the gen_func() when it is called for the first time it returns ‘0’ as the value. Next, when it is called, the value returned previously is incremented by 1 as inside the function’s code, and then it returns ‘1’. Again the value of x is incremented and returns ‘2’ as a value; this loop continues till less than 5 as mentioned in the while loop above in the code. So it prints values ‘0’, ‘1’, ‘2’, ‘3’, ‘4’.
The main points why yield statements can be used instead of the return statement:
- Firstly we can easily create a function that is iterable using yield, which is also called a generator function.
- Whenever there are continuous calls made to a function, it starts from the last yield statement itself, so you can again save time.
- Lastly but very important, the yield statement is used when you want to return more than one value from the function. So the return value from the yield statement stores data in a local state so that the allocation of memory is also saved and how every time the different value is returned. Hence from this, even the memory is also saved.
In this topic, when the function is called after it has completed the loop, then we will get an error, and this error can be caught and raise the error by using the next() method, which can be shown in the below example.
def yield_func(l): total = 0 for n in l: yield total total += n new_lst = yield_func() print(next(new_lst)) print(next(new_lst)) print(next(new_lst))
So in the above, we get StopIteration error, and it can be done using the next() method. This is done as below.
def yield_func(l): total = 0 for n in l: yield total total += n new_lst = yield_func([10,20,30]) print(next(new_lst)) print(next(new_lst)) print(next(new_lst))
Like other programming languages, Python can return a single value, but in this, we can use yield statements to return more than one value for the function. The function that uses the yield keyword is known as a generator function. So this function can be used when you want the iterable values to be returned. The yield statement not only helps to return more than one value, but it also saves time and memory by using more functions, and it can save the memory as every time the function is called, it stores its value in local memory, and it uses it again for the next call.
We hope that this EDUCBA information on “Python yield” was beneficial to you. You can view EDUCBA’s recommended articles for more information.