Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 yield help chakox Unladen Swallow Posts: 3 Threads: 1 Joined: Apr 2019 Reputation: 0 Likes received: 0 #1 Apr-11-2019, 11:28 PM (This post was last modified: Apr-11-2019, 11:28 PM by chakox. Edited 1 time in total.) 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 `````` perfringo Da Bishop Posts: 1,360 Threads: 7 Joined: Jun 2018 Reputation: 138 Likes received: 300 #2 Apr-12-2019, 06:24 AM 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. Gribouillis Posts: 2,282 Threads: 25 Joined: Jan 2018 Reputation: 216 Likes received: 548 #3 Apr-12-2019, 07:02 AM 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 ``` chakox Unladen Swallow Posts: 3 Threads: 1 Joined: Apr 2019 Reputation: 0 Likes received: 0 #4 Apr-13-2019, 09:00 PM Thanks it work but i don't understand why the other method work only in the interpreter. Gribouillis Posts: 2,282 Threads: 25 Joined: Jan 2018 Reputation: 216 Likes received: 548 #5 Apr-13-2019, 09:26 PM (This post was last modified: Apr-13-2019, 09:26 PM by Gribouillis. Edited 1 time in total.) 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. chakox Unladen Swallow Posts: 3 Threads: 1 Joined: Apr 2019 Reputation: 0 Likes received: 0 #6 Apr-13-2019, 09:42 PM Ok it's what that, thanks. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Problem about yield, please help!! cls0724 5 248 Apr-08-2020, 05:37 PM Last Post: deanhystad does yield support variable args? Skaperen 0 154 Mar-03-2020, 02:44 AM Last Post: Skaperen generator function that yield from a list buran 9 832 Jun-04-2019, 10:26 PM Last Post: snippsat Multiple calls to Python interpreter embedded in C++ application yield segmentation f mmoelle1 0 595 Mar-21-2019, 08:54 PM Last Post: mmoelle1 1 == 1 seems to yield false zatlas1 3 597 Jan-07-2019, 01:49 PM Last Post: Gribouillis Yield Keyword 6pathsMadara 6 1,009 Aug-24-2018, 11:43 AM Last Post: 6pathsMadara what does "yield '\n'" do? 2sky 2 1,413 Jan-30-2018, 09:35 AM Last Post: 2sky

Forum Jump:

Users browsing this thread: 1 Guest(s)