Chapter 3Generators
Generators allow sequences of values to be handled while computing each value of the sequence only as it is needed, rather than as a traditional list (which must compute all of its values ahead of time).
Using generators where appropriate can result in substantial memory savings, because large collections of data do not need to be stored in memory in their entirety. Similarly, generators are uniquely able to handle representation of some sequences that cannot be accurately represented by lists.
This chapter explains what a generator is, and the syntax for using generators in Python. It also covers some of the common generators that are provided in the Python standard library.
Understanding What a Generator Is
A generator is a function that, instead of executing and returning a single value, sends back one or more values in a sequence. A generator function executes until it is told to yield a value, and then it continues execution until told to do so again. This continues until the function is complete, or until iteration over that generator terminates.
There is no explicit requirement that a generator terminate at all; generators may represent infinite sequences. There is nothing inherently wrong with this. In cases where this occurs, it is simply the responsibility of the code iterating over the generator to break out of the sequence when appropriate (such as with a break statement).
Understanding Generator Syntax
A generator function is recognizable ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access