Updated August 10, 2023
Difference Between Python Yield vs Return
The yield statement is used in Python to replace the return of a function to send a value back to its caller without destroying local variables, while Return Statement exits from a function, handing back value to its caller. So when using yield statement in the function, then that function is called generator function, which returns generator objects means generators are iterators as they do not save all the values in memory so only once you can iterate over them. The return statement is used when a function wants to return some value when it is called. So this statement always stops the execution of the function and returns the value.
Head to Head Comparison between Python Yield vs Return (Infographics)
Below are the top 10 comparisons between Python Yield vs Return:
Key differences between Python Yield vs Return
Let us discuss some key differences between Python Yield vs Return in the following points:
- The return statement returns the value back to the caller. The yield statement also returns the value to the caller but also saves it in the current state.
- The code written after the return statement is not executed if they are in the function body. But this problem can be overcome by yield statement, which they use next() method to read the next code after the yield statement and can execute them.
- The function having a return statement runs the function call from the beginning or start of the function. The function with a yield statement can execute the function from the last state where it was paused to return the value and return the next value.
- In the function that has a return, a statement can return only one value. In the function that has the yield, a statement can return a series of values.
- In the normal function, the return statements are used to cause the exit of the function. The functions with yield statement are used to define generators.
- The return statement is always written at the end of the function, whereas the yield statement can be written inside the function where you want to return the values, and we can continue the code after the yield statement and can read the next commands after the yield statement using next() method.
Comparison Table of Python Yield vs Return
The table below summarizes the comparisons between Python Yield vs Return:
Yield Statement |
Return Statement |
It is used in generator functions. | It is used in normal functions. |
It returns a series of values. | It returns only one value. |
It sends the value to the caller without destroying local variables. | It sends back the value to the caller and exits from the function destroying local variables. |
This suspends the execution and returns the value to the caller, and resumes again. | This stops the execution and returns the value to the caller exits out of the function. |
It can run multiple times. | It runs only one time. |
Code written after any yield statement can be executed using the next() function. | The Code written after the return statement cannot be executed. |
This statement can be executed from where it was last stopped when the functions get paused. | This statement calls the function, or it runs the function only from starting only. |
In the function that uses the yield, the statement can have more than one yield statement or even a single yield statement to return values. Therefore in the generator function, it can return a series of values. | In the normal function, which uses a return statement, can have only one return statement per function; it cannot have more than one return in a single loop or function. Therefore in the normal function, it can return a single value. |
These yield statements are used in functions known as generator functions. In Python, these generator functions are used to develop iterators that produce a sequence of results, and this process of producing results can be paused and resumed dynamically. | These return statements are used in functions known as a normal function which is defined similarly as other languages. In Python, this function can return values by using the return statement. |
This process of suspension of function and resuming after returning one value can be done only by yield statement. | But these statements usually execute the entire function and exit the function by returning the value back to the caller. It cannot suspend or resume after execution; it directly exits from the function. Hence it can return a single value, and each time it will call the function. |
Conclusion
In Python, like other languages, the function is used to return values using the return statement. This return statement can return a single value, and it stops the execution of the function when the function hits the return statement, and it usually starts the function call from starting of the function. To overcome all this, Python has introduced a yield statement in which it can be used in the normal function instead of a return statement, but such functions are called generator functions. This yield statement can return a series of values, and this suspends the execution of the function call and returns the value without destroying the local variable and again resumes the function where it had last stopped.
Recommended Articles
This is a guide to the top differences between Python Yield vs Return. Here we discuss the Python Yield vs Return key differences with infographics and comparison table. You may also have a look at the following articles to learn more –