Linked Questions

93 votes
5 answers
11k views

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 ...
Paul's user avatar
  • 6,034
28 votes
3 answers
26k views

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....
unnobtainium's user avatar
21 votes
4 answers
5k views

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 ...
Dewclaw's user avatar
  • 221
8 votes
4 answers
16k views

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 ...
GeorgePotter's user avatar
14 votes
3 answers
7k views

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 ...
Niels's user avatar
  • 1,533
28 votes
1 answer
1k views

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): ...
Santanu C's user avatar
  • 1,402
5 votes
1 answer
4k views

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 ...
user2878309's user avatar
4 votes
2 answers
273 views

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 ...
Anshul Vyas's user avatar
6 votes
0 answers
3k views

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) : ...
Sebastien D's user avatar
  • 4,502
1 vote
4 answers
1k views

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 ...
user avatar
-2 votes
3 answers
3k views

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 ...
rupert's user avatar
  • 9
-2 votes
2 answers
720 views

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&...
somniumm's user avatar
  • 115
0 votes
1 answer
444 views

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 ...
Pomegranate Molasses's user avatar
5 votes
2 answers
276 views

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 ...
Graham Morehead's user avatar
1 vote
2 answers
351 views

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 ...
user avatar

15 30 50 per page
1
2 3 4 5
35