Credit: Raymond Hettinger
Lather, Rinse, Repeat
—Docs for my bottle of shampoo
After namespaces, iterators and generators emerged as the next “honking great ideas” in Python. Since their introduction in Python 2.2, they have come to pervade and unify the language. They encourage a loosely coupled programming style that is simple to write, easy to read, flexible, and extendable.
Simply put, the iterator protocol has two halves, a producer and a consumer. An iterable object says, “I know how to supply data one element at a time,” and the consumer says “please give me data one element at a time and say Stop when you’re done.”
The producer/consumer connection can appear in a number of
guises. The simplest is where a function or constructor wraps around
an iterable object. For example,
sorted(set('simsalabim')) has the set
constructor looping over the elements of the iterable string and a
sorted function wrapping around the
resulting iterable set object.
In addition to functions and constructors, regular Python
statements can use the
to loop over iterable objects.
for line in
myfile: print line loops over lines of an iterable
file object. Likewise,
if token in sequence loops over elements of
a sequence until it finds a match (or until it reaches the end with no
Both guises of the consumer side of the iterator protocol use the protocol implicitly. In addition, an explicit form is ...