Generator (computer science)

From Academic Kids

pl:Generator (informatyka) In computer science, a generator is a special routine that can be used to control the iteration behaviour of a loop. The concept was invented for the Sather programming language but it has also been incorporated in the Python programming language.

Generators can only be invoked inside loops. The first time that a generator invocation is reached in a loop, an iterator object is created that encapsulates the state of the generator routine at its beginning, with arguments bound to the corresponding parameters. The generator's body is then executed in the context of that iterator until a special yield action is encountered; at that time, the value provided with the yield action is used as the value of the invocation expression. The next time the same generator invocation is reached in a subsequent iteration, the execution of the generator's body is resumed after the yield action, until a yet another yield action is encountered. In addition to the yield action, execution of the generator body can also be terminated by a finish action, at which time the innermost loop enclosing the generator invocation is terminated.

In the presence of generators, loop constructs of a language can be reduced into a single loop ... end loop construct; all the usual loop constructs can then be comfortably simulated by using suitable generators in the right way.

In the Python programming language, a generator is a special type of continuation that can be used as an iterator.

An example Python generator:

def sums(n, min=1):
    if n == 0:
        yield []
    elif n >= min:
        for i in range(min, n+1):
            for sol in sums(n-min, i):
                yield [i] + sol

In Python, a generator can be thought of as an iterator that contains a frozen function call. Whenever the iterator's next() method is called, the frozen function call resumes where it was left off and runs until it reaches a yield statement. Then the function call is frozen again, the iterator spits out the yielded value, and execution continues with the iterator's caller.

Generators can be used to loop through the values of a list lazily. This is useful when only the first few items of the list are likely to be needed, and calculating the entire list would be costly or impractical.


  • Stephan Murer, Stephen Omohundro, David Stoutamire and Clemens Szyperski: Iteration abstraction in Sather. ACM Transactions on Programming Languages and Systems, 18(1):1-15 (1996) [1] (

Academic Kids Menu

  • Art and Cultures
    • Art (
    • Architecture (
    • Cultures (
    • Music (
    • Musical Instruments (
  • Biographies (
  • Clipart (
  • Geography (
    • Countries of the World (
    • Maps (
    • Flags (
    • Continents (
  • History (
    • Ancient Civilizations (
    • Industrial Revolution (
    • Middle Ages (
    • Prehistory (
    • Renaissance (
    • Timelines (
    • United States (
    • Wars (
    • World History (
  • Human Body (
  • Mathematics (
  • Reference (
  • Science (
    • Animals (
    • Aviation (
    • Dinosaurs (
    • Earth (
    • Inventions (
    • Physical Science (
    • Plants (
    • Scientists (
  • Social Studies (
    • Anthropology (
    • Economics (
    • Government (
    • Religion (
    • Holidays (
  • Space and Astronomy
    • Solar System (
    • Planets (
  • Sports (
  • Timelines (
  • Weather (
  • US States (


  • Home Page (
  • Contact Us (

  • Clip Art (
Personal tools