Python Forum
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
Reply
#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.
Reply
#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
Reply
#4
Thanks it work but i don't understand why the other method work only in the interpreter.
Reply
#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.
Reply
#6
Ok it's what that, thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  yield usage as statement or expression akbarza 5 750 Oct-23-2023, 11:43 AM
Last Post: Gribouillis
  Using list comprehension with 'yield' in function tester_V 5 1,175 Apr-02-2023, 06:31 PM
Last Post: tester_V
  Yield generator weird output Vidar567 8 3,196 Nov-23-2020, 10:59 PM
Last Post: deanhystad
  Trying to access next element using Generator(yield) in a Class omm 2 1,932 Oct-19-2020, 03:36 PM
Last Post: omm
  Yield statement question DPaul 6 2,447 Sep-26-2020, 05:18 PM
Last Post: DPaul
  Problem about yield, please help!! cls0724 5 2,810 Apr-08-2020, 05:37 PM
Last Post: deanhystad
  does yield support variable args? Skaperen 0 1,641 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  generator function that yield from a list buran 9 4,101 Jun-04-2019, 10:26 PM
Last Post: snippsat
  Multiple calls to Python interpreter embedded in C++ application yield segmentation f mmoelle1 0 2,792 Mar-21-2019, 08:54 PM
Last Post: mmoelle1
  1 == 1 seems to yield false zatlas1 3 2,545 Jan-07-2019, 01:49 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020