Python Forum
yield help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: yield help (/thread-17460.html)



yield help - chakox - Apr-11-2019

hey everyone !
So I'm currently testing yield function
#contient des exemples d'utilisation de la fonction Yield

#exp 1
def exYield1() :
    """test de yield"""
    yield 1
    yield 2
    yield 3

    
print("Exemple 1 : ")
for i in exYield1() :
    print(i)
print("")

#exp 2
def exYield2(bord_inf,bord_sup) :
    """Cette fonction avance de 2 en 2 entre des bords délimités"""
    while bord_inf < bord_sup :
        valeurRecu = (yield bord_inf)
        if valeurRecu is not None :
            bord_inf = valeurRecu
        bord_inf += 2
        
print("Exemple 2 : ")       
#compte de 2 en 2 de 10 à 20
for i in exYield2(10,20) :
    print(i)
print("")

print("Exemple 3 : ")
#compte de 2 en 2 de 10 à 50
gen1 = exYield2(10,100)
for i in gen1 :
    print(i)
    if i == 50 :
        gen1.close()
print("")

print("Exemple 4 : ")
#compte de 2 en 2 de 10 à 100 mais saute les nombres de 20 à 59 
gen2 = exYield2(10,100)
for i in gen2 :
    print(i)
    if i == 18 :
        gen2.send(58)
and when i get to the 4th exemple the loop does not display the number 60
Output:
Exemple 4 : 10 12 14 16 18 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98
it only append when i launch the script, if i rewrite the code in the console(exyield2 and the 4th exemple)60 is display.
Output:
10 12 14 16 18 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98



RE: yield help - perfringo - Apr-12-2019

I observe that if 56 is sent instead of 58 one will get 60 too. You should look into definition of you function to find out why this behaviour happens.

Python documentation about yield and yield expression


RE: yield help - Gribouillis - Apr-12-2019

It is because send() runs the generator to the next yield statement, just like next(). You can get the correct result with this code
def exYield2(bord_inf,bord_sup) :
    """Cette fonction avance de 2 en 2 entre des bords délimités"""
    valeurRecu = None
    while bord_inf < bord_sup :
        if valeurRecu is not None :
            bord_inf = valeurRecu
        valeurRecu = (yield bord_inf)
        bord_inf += 2



RE: yield help - chakox - Apr-13-2019

Thanks it work but i don't understand why the other method work only in the interpreter.


RE: yield help - Gribouillis - Apr-13-2019

chakox Wrote:i don't understand why the other method work only in the interpreter.
When you run code in the interactive interpreter, it prints the result of every statement (if not None). In this case, it prints the result of gen2.send(58), which is the next value produced by yield. In a file you don't see the result because there is no call to print(). Consider this
>>> x = y = 1
>>> x + y
2
The same code in a file doesn't display the 2.


RE: yield help - chakox - Apr-13-2019

Ok it's what that, thanks.