What is Yield for?

For some time now I have been writing some basic scripts with Python, in some tutorials I am sometimes presented with yield, which usually appears in repeat structures commonly while. What's it for? And how to use?

Author: Maniero, 2015-10-16

2 answers

Python has its own specificities but essentially in all languages they work the same. It creates a generator, that is, it creates a list of data that is consumed on demand. In general it is used to give better abstractions to the code. Everything you do with it, you can do without it in a very similar way, but exposing the data generation mechanism.

It returns a value keeping the state from where it left off. When it runs again it continues from where it left off. It controls the state of an enumerator between function executions.

def impar(elems):
    for i in elems:
        if i % 2:
            yield i

for x in impar(range(1000)):

Source .

This code will print all the odd ones from 0 to 1000. It will call the method 500 times, each time will bring an odd number. The compiler / library will assemble the internal structure to know what number it is on each call.

Of course it has a hidden variable that survives beyond the function's internal environment. So this i doesn't start again on every function call. Note that you can call the function impar() without knowing how it does the selection internally.

This example is simple and obvious, but think of more complex things. You abstract better. You say it will call a method that filters the odd ones for you. It doesn't matter how. What you will do with the information generated is your problem. The function with yield has sole responsibility for generating the information.

Another example:

def numeros():
    yield 1
    yield 2
    yield 3

print numeros()
print numeros()
print numeros()

This will print 1, 2 and 3.

See C # explanations and PHP.

 28
Author: Maniero, 2019-10-23 13:32:28

Complementing Maniero's answer, some features of yield:

Yield in Python it is used whenever you need to define a generator function for something. You can not use yield out of a generator function.

When you use the statement yield within a function, it turns into a generating function.

Generating function is a function that returns more than one Value, Returns a series of values.

A common use is within loops such as:

for value in simpleGeneratorFun():  
     print(value)  

And the declaration of our generating function would be something like this:

def simpleGeneratorFun():
    for i in range (1, 4):
        yield i

Whenever the instruction yield occurs within the generating function, the program pauses execution and returns the value to the caller, in the example above, it returns a value in for.

O yield retains the state of the function where it is paused (when returning the value).

Next time, when a caller calls the generating function (in our case, the for), the function body will execute the statement from where it was paused, instead of executing the first statement.

You can call the generating function as long as it has not reached its last statement.

That is, the first time you call simpleGeneratorFun() it will return 1, in the second, it will return 2, until it reaches 4

Summarized, a little edited for better understanding and translated from:
https://www.csestack.org/python-yield-vs-return-explained-detail-examples /

 6
Author: danilo, 2021-01-08 21:57:23