abstractions-in-python.github.io

Generators

Suppose you have to produce a sequence of data, such as words from some text. One idea is to generate them all at once. Another idea is to write a generator, which generates a word each time it is needed. By writing a generator, you can leave it up to the process that uses words to decide how many of them they will want to receive.

Generators are a basic notion of Python, and are instrumental in the implementation of iterators. Here we present them, along with some applications.

Notebook

Video

Possible problem sets