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 values from the function called. As in any programming language if we execute a function and it needs to perform some task and have to give its result so 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.
yield statement (s)
Examples of Python yield
Let’s take a simple and easy example to understand the yield statement:
for y in yield_function():
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 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 as it needs to print the iterable values returned by the functions.
n = range(3)
for i in n:
gen = yield_func()
for i in gen:
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 that has the code which demonstrates of generating generator object and printing the return values using a loop.
x = 0
while x < 5:
x += 1
print("Simple function call without using loop:\n")
print("Below is with using a loop:")
for y in gen_func():
In this above code the gen_func() when it is called for the first time it returns ‘0’ as the value, and next time when it is called the value returned previously is incremented by 1 as inside the code of function and then it returns ‘1’ and again the value of x is incremented and returns ‘2’ as 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 next() method which can be shown in the below example.
total = 0
for n in l:
total += n
new_lst = yield_func()
So in the above, we get StopIteration error and it can be done using next() method. This is done as below.
total = 0
for n in l:
total += n
new_lst = yield_func([10,20,30])
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 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.
This is a guide to Python yield. Here we discuss a brief overview on Python yield Statement and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –