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.

5993 questions
47 answers

What does the "yield" keyword do?

What is the use of the yield keyword in Python? 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.
  • 134,175
  • 17
  • 51
  • 61
13 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.
  • 7,039
  • 3
  • 15
  • 8
12 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)]
  • 321,454
  • 105
  • 202
  • 204
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
  • 83,870
  • 149
  • 415
  • 613
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
  • 32,268
  • 29
  • 96
  • 145
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…
  • 37,008
  • 50
  • 189
  • 279
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,981
  • 4
  • 56
  • 74
9 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…
  • 303,436
  • 70
  • 517
  • 542
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…
  • 3,800
  • 4
  • 19
  • 19
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…
  • 31,649
  • 37
  • 91
  • 121
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,271
  • 2
  • 13
  • 3
16 answers

What can you use 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.
  • 32,286
  • 12
  • 51
  • 67
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…
  • 47,598
  • 97
  • 279
  • 443
9 answers

What's the purpose of "send" function on Python generators?

Can someone give me an example of why the "send" function associated with Python generator function exists? I fully understand the yield function. However, the send function is confusing to me. The documentation on this method is convoluted:…
  • 10,936
  • 11
  • 54
  • 88
23 answers

How do I know if a generator is empty from the start?

Is there a simple way of testing if the generator has no items, like peek, hasNext, isEmpty, something along those lines?
  • 31,393
  • 22
  • 57
  • 85
2 3
99 100