Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 yield help
#1
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
Quote
#2
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#3
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
Quote
#4
Thanks it work but i don't understand why the other method work only in the interpreter.
Quote
#5
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.
Quote
#6
Ok it's what that, thanks.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  does yield support variable args? Skaperen 0 99 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  generator function that yield from a list buran 9 730 Jun-04-2019, 10:26 PM
Last Post: snippsat
  Multiple calls to Python interpreter embedded in C++ application yield segmentation f mmoelle1 0 464 Mar-21-2019, 08:54 PM
Last Post: mmoelle1
  1 == 1 seems to yield false zatlas1 3 517 Jan-07-2019, 01:49 PM
Last Post: Gribouillis
  Yield Keyword 6pathsMadara 6 908 Aug-24-2018, 11:43 AM
Last Post: 6pathsMadara
  what does "yield '\n'" do? 2sky 2 1,296 Jan-30-2018, 09:35 AM
Last Post: 2sky

Forum Jump:


Users browsing this thread: 1 Guest(s)