In Python, the definition of generator is done with the help of yield statement. So before we get into the details of what yield is actually used for, it's important to understand an overview of generators. If you're new to Python, there's a good chance you've used a Python generator before. Generators play an important role in Python. In Python, iterators can be generated using generators, but the process takes a slightly different form.
Python generators are functions that can be paused and resumed dynamically and create a sequence of results. They can also be used to generate random numbers. In Python 2.2, generators were first introduced as an optional feature. In Python 2.3, they are enforced. Generator functionality has been greatly improved in Python 2.5, although they already have sufficient functionality.
To maintain backward compatibility, the addition of generators in Python 2.2 resulted in the introduction of a new keyword called "yield". In order to use generators, we need to import them from the _future_ module. When generators became the default in the Python 2.3 release, this was changed to reflect the fact that the change was no longer needed.
Use the yield statement to temporarily stop the execution of a function, which then returns a value to the caller while saving the function's state for later restoration. This means that the entire generator can still be restarted after getting the return value. Execution of a function terminates with a return statement, which also returns a value to the person who called the function. If missing, your function will return nothing.
In Python generators, the yield statement replaces the function's return so that a value is returned to the person who called the generator without deleting any local variables. In order to better understand the function of yield statement execution in Python programming, you first need to be familiar with generators.
The difference between a generator function and an ordinary function is that there is a "yield" statement in the definition of the generator function. It begins with the "yield" keyword, which identifies the generator object to be returned to whoever calls this function.
In Python, a specific type of function called a "generator" returns not a data value to the person who called the function, but another generator object. With the yield keyword, you can temporarily stop the execution of a function, save the state, and resume the function later.
Look at the example below -
# Use of yield def printresult(String): for i in String: if i == "p": yield i # Initializing string String = "Happy Birthday" ans = 0 print ("The number of 'p' in word is: ", end = "" ) String = String.strip() for j in printresult(String): ans = ans + 1 print (ans)
The number of 'p' in word is: 2
What is Python Return?
In contrast to the yield statement, the return statement causes the function to terminate while passing the value back to the function that called it. Functions that are more procedural in nature do not return anything explicitly to the caller, but instead return a value that is sent back to the calling function. Although a function can have multiple return statements, only one of them can be called for each call to each of those statements.
Almost always place the return statement at the very end of a function block, its purpose is to return the final result of executing all statements contained in the function. However, a return statement may also appear before a function block to stop execution of all subsequent statements in the block. This can occur if it is used to prevent the execution of a function. This causes the caller to immediately restart the execution of the program. The "None" return object type is equivalent to that in Python when no value is provided for the return object.
The following example shows the use of return in Python -
# Show return statement class Test: def __init__(self): self.str = "Happy Birthday" self.x = "Pradeep" # This function returns an object of Test def fun(): return Test() # Driver code to test above method t = fun() print(t.str) print(t.x)
Happy Birthday Pradeep
The following table highlights the main differences between Yield and Return in Python -
Basic comparison | Yield | return |
---|---|---|
Base | In most cases, you will need to use the yield function to convert a typical Python function into a generator. | In most cases, the end of execution is signaled by using the return keyword, which "returns" the result to the statement that called it. |
Function | It replaces the function's return in order to pause the execution of the function without losing any local variables. | It exits the function and returns a value to its caller. |
use | The caller will use this function when the generator provides intermediate results to the caller. | When a function is preparing to pass a value, it is necessary to use this. |
implement | The code written after theyield statement will be executed in the following function call. | Although the code written after the return statement will not be executed. |
Compile | It has the ability to run multiple times. | It only runs once at a time. |
yield statement generates a generator object and can return multiple values to the caller without terminating the program, while the return statement is used to return a value from inside the function, it terminates the program. The return statement is used to return a value from within a function to the caller.
The above is the detailed content of In Python, what is the difference between Yield and Return?. For more information, please follow other related articles on the PHP Chinese website!