Generator

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. http://en.wikipedia.org/wiki/Generator_%28computer_science%29

Python-specific bits

  • In Python, a generator can be thought of as an iterator that contains a frozen stack frame. Whenever the iterator's next() method is called, Python resumes the frozen frame, which executes normally until the next yield statement is reached. The generator's frame is then frozen again, and the yielded value is returned to the caller. Generators can be implemented in terms of more expressive control flow constructs, such as Co Routine-s or first-class Continuation-s.

Edited:    |       |    Search Twitter for discussion