Linked Questions
523 questions linked to/from What does the "yield" keyword do in Python?
93
votes
5
answers
11k
views
for x in y(): how does this work? [duplicate]
I was looking for code to spin a cursor in the terminal and found this. I was wondering what was happening in the code. In particular for c in spinning_cursor(): I've never seen this syntax. Is it ...
28
votes
3
answers
26k
views
How two consecutive yield statement work in python? [duplicate]
I stumble upon this code from pymotw.com in merging and splitting section.
from itertools import *
def make_iterables_to_chain():
yield [1, 2, 3]
yield ['a', 'b', 'c']
for i in chain....
21
votes
4
answers
5k
views
What is a "yield" statement in a function? [duplicate]
Possible Duplicate:
The Python yield keyword explained
Can someone explain to me what the yield statement actually does in this bit of code here:
def fibonacci():
a, b = 0, 1
while ...
8
votes
4
answers
16k
views
What does yield do in python 2.7? [duplicate]
Possible Duplicate:
The Python yield keyword explained
Okay, I've probably phrased the question badly but this is the situation I have.
I have this line of code in Python 2.7 which I'm trying to ...
14
votes
3
answers
7k
views
How does a function in Python remember its values after it returns? [duplicate]
Warning: extreme newbie question
I seem to have been thinking of functions as a recipe. In my world, the program is a recipe box and some of the recipes (functions) call for other recipes (other ...
28
votes
1
answer
1k
views
Trouble understanding python generators [duplicate]
I am new to generator in python. I have a simple enough code which I am playing with but I can not understand the output I am getting out of it. Here is my code :
def do_gen():
for i in range(3):
...
5
votes
1
answer
4k
views
Yield does not work, but return does [duplicate]
I'm making a Python irc bot. For some reason the yield statement in my join() method makes it skip the method altogether, but if I replace it with a return it works fine. However, I need to yield an ...
4
votes
2
answers
273
views
How Generator expressions work internally in python? [duplicate]
I have tried this following code:
result = (x for x in range(3))
for y in result:
print(y)
I am getting the following Output:
0
1
2
But when I am using this code :
result = (print(x) for x in ...
6
votes
0
answers
3k
views
What is the difference between yield and return? [duplicate]
I've read that yield is a generator but I can't really see what it implies since the output of the following function is exactly the same:
def yieldFunction(number):
for x in range(number) :
...
1
vote
4
answers
1k
views
What does "for i in generator():" do? [duplicate]
Can someone explain what each step in this does?
I have never seen "for i in X:" used where X is a generator, and I am failing to understand how the i interacts with the function if it's not being ...
-2
votes
3
answers
3k
views
python print inside vs outside function? [duplicate]
the below code had previously printed the fibonnacci using for loop at end. I moved the print inside the function and then called it but no fibonacci nubers were printed out in spite of the print ...
-2
votes
2
answers
720
views
Why does my generator function doesn't loop? [duplicate]
I'm creating a python generator to loop over a sentence. "this is a test" should return
this
is
a
test
Question1: What's the problem with the implementation below? It only return "this&...
0
votes
1
answer
444
views
Why is this generator not working in Python? [duplicate]
I am trying to return all product reviews by user name.
I have written a generator function but on coming back to it, it does not work and returns a syntax error:
def find_reviews(username):
for u ...
5
votes
2
answers
276
views
Unreachable 'yield' still strangely affects function [duplicate]
If you run the following snippet, it will ignore the 'print' on line 2 and 'exit' on line 3. However, if you comment out the already unreachable 'yield' from line 4, lines 2 and 3 will execute ...
1
vote
2
answers
351
views
Yield and then return or just return? [duplicate]
I have a generator and want to yield none after which I want the iteration of the generator to stop, i.e. StopIteration but I am not sure which way to accomplish this.
I think of the following to ...