8.1 Working with the infinite iterators
The itertools
module provides a number of functions that we can use to enhance or enrich an iterable source of data. We’ll look at the following three functions:
count()
: This is an unlimited version of therange()
function. An upper bound must be imposed by the consumer of this sequence.cycle()
: This will reiterate a cycle of values. The consumer must decide when enough values have been produced.repeat()
: This can repeat a single value an indefinite number of times. The consumer must end the repetition.
Our goal is to understand how these various iterator functions can be used in generator expressions and with generator functions.
8.1.1 Counting with count()
The built-in range()
function is defined by an upper limit: the lower limit and step values are optional. The count()
function, on the other hand, has a start and optional step, but no upper limit.
This function can be thought of as the primitive basis for a function such as...