Questions tagged [generator]

A generator is a generalisation of a subroutine, primarily used to simplify the writing of iterators. The yield statement in a generator does not specify a coroutine to jump to, but rather passes a value back to a parent routine.

From Wikipedia:

A generator is a special routine that can be used to control the iteration behaviour of a loop. A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values. However, instead of building an array containing all the values and returning them all at once, a generator yields the values one at a time, which requires less memory and allows the caller to get started processing the first few values immediately. In short, a generator looks like a function but behaves like an iterator.

Generators can be implemented in terms of more expressive control flow constructs, such as coroutines or first-class continuations.

5713 questions
11167
votes
38 answers

What does the "yield" keyword do?

What is the use of the yield keyword in Python, and what does it do? For example, I'm trying to understand this code1: def _get_child_candidates(self, distance, min_dist, max_dist): if self._leftchild and distance - max_dist < self._median: …
Alex. S.
  • 126,349
  • 16
  • 50
  • 61
617
votes
12 answers

Difference between Python's Generators and Iterators

What is the difference between iterators and generators? Some examples for when you would use each case would be helpful.
newToProgramming
  • 6,309
  • 3
  • 15
  • 8
493
votes
57 answers

Random color generator

Given this function, I want to replace the color with a random color generator. document.overlay = GPolyline.fromEncoded({ color: "#0000FF", weight: 10, points: encoded_points, zoomFactor: 32, levels: encoded_levels, …
n00ki3
  • 13,009
  • 15
  • 52
  • 65
447
votes
10 answers

Generator expressions vs. list comprehensions

When should you use generator expressions and when should you use list comprehensions in Python? # Generator expression (x*2 for x in range(256)) # List comprehension [x*2 for x in range(256)]
readonly
  • 306,152
  • 101
  • 198
  • 201
359
votes
8 answers

How to take the first N items from a generator or list?

With linq I would var top5 = array.Take(5); How to do this with Python?
Jader Dias
  • 81,082
  • 147
  • 410
  • 611
322
votes
12 answers

Lazy Method for Reading Big File in Python?

I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece. Is there any method to yield these pieces…
Pratik Deoghare
  • 30,271
  • 27
  • 94
  • 143
311
votes
8 answers

Ruby on Rails generates model field:type - what are the options for field:type?

I'm trying to generate a new model and forget the syntax for referencing another model's ID. I'd look it up myself, but I haven't figured out, among all my Ruby on Rails documentation links, how to find the definitive source. $ rails g model Item…
Meltemi
  • 36,348
  • 48
  • 182
  • 274
287
votes
7 answers

Can I use ES6's arrow function syntax with generators? (arrow notation)

That is, how do I express function *(next) {} with arrow syntax? I've tried all the combinations I could think of, and I can't find any documentation on it. (I am currently using Node.js v0.11.14.)
Ashley Coolman
  • 9,223
  • 4
  • 47
  • 68
261
votes
8 answers

What does yield mean in PHP?

I've recently stumbled over this code: function xrange($min, $max) { for ($i = $min; $i <= $max; $i++) { yield $i; } } I've never seen this yield keyword before. Trying to run the code I get Parse error: syntax error, unexpected…
Gordon
  • 296,205
  • 68
  • 508
  • 534
257
votes
8 answers

How to pick just one item from a generator?

I have a generator function like the following: def myfunct(): ... yield result The usual way to call this function would be: for r in myfunct(): dostuff(r) My question, is there a way to get just one element from the generator whenever I…
Alexandros
  • 3,529
  • 3
  • 18
  • 19
231
votes
13 answers

Understanding generators in Python

I am reading the Python cookbook at the moment and am currently looking at generators. I'm finding it hard to get my head round. As I come from a Java background, is there a Java equivalent? The book was speaking about 'Producer / Consumer', however…
Federer
  • 30,291
  • 37
  • 89
  • 120
219
votes
16 answers

What can you use Python generator functions for?

I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.
quamrana
  • 27,260
  • 11
  • 50
  • 62
209
votes
13 answers

How to join two generators in Python?

I want to change the following code for directory, dirs, files in os.walk(directory_1): do_something() for directory, dirs, files in os.walk(directory_2): do_something() to this code: for directory, dirs, files in os.walk(directory_1) +…
Homer Xing
  • 2,091
  • 2
  • 12
  • 3
197
votes
22 answers

Is there a mechanism to loop x times in ES6 (ECMAScript 6) without mutable variables?

The typical way to loop x times in JavaScript is: for (var i = 0; i < x; i++) doStuff(i); But I don't want to use the ++ operator or have any mutable variables at all. So is there a way, in ES6, to loop x times another way? I love Ruby's…
at.
  • 45,606
  • 92
  • 271
  • 433
181
votes
5 answers

How do I build a numpy array from a generator?

How can I build a numpy array out of a generator object? Let me illustrate the problem: >>> import numpy >>> def gimme(): ... for x in xrange(10): ... yield x ... >>> gimme() >>> list(gimme()) [0, 1, 2, 3, 4, 5,…
saffsd
  • 21,564
  • 16
  • 57
  • 65
1
2 3
99 100