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.

6407 questions
50 answers

What does the "yield" keyword do in Python?

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.
  • 143,260
  • 19
  • 55
  • 62
15 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,905
  • 3
  • 17
  • 8
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
  • 88,211
  • 155
  • 421
  • 625
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
  • 35,497
  • 30
  • 100
  • 146
8 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
  • 11,095
  • 5
  • 59
  • 81
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…
  • 4,425
  • 4
  • 23
  • 21
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,979
  • 50
  • 195
  • 293
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…
  • 312,688
  • 75
  • 539
  • 559
15 answers

How to join two generators (or other iterables) 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,681
  • 2
  • 16
  • 3
24 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…
  • 50,922
  • 104
  • 292
  • 461
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…
  • 33,677
  • 39
  • 93
  • 121
9 answers

What is the purpose of the "send" function on Python generators?

I understand yield. But what does a generator's send function do? The documentation says: generator.send(value) Resumes the execution and “sends” a value into the generator function. The value argument becomes the result of the current yield…
  • 12,588
  • 14
  • 59
  • 110
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.
  • 37,849
  • 12
  • 53
  • 71
25 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?
  • 33,953
  • 24
  • 61
  • 87
10 answers

How to check if an object is a generator object in python?

In python, how do I check if an object is a generator object? Trying this - >>> type(myobject, generator) gives the error - Traceback (most recent call last): File "", line 1, in NameError: name 'generator' is not defined (I…
Pushpak Dagade
  • 6,280
  • 7
  • 28
  • 41
2 3
99 100