---

developerWorks: Using Combinatorial Functions in the itertools Module

“The idea of iterators was introduced into Python with version
2.2. Well, that is not quite true; hints of the idea were already
present in the older function xrange(), and the file method
.xreadlines(). Python 2.2 generalized the notion in much of its
internal implementation and made programming custom iterators much
more straightforward by introducing the yield keyword (the presence
of yield turns a function into a generator, which in turn returns
an iterator).

“The motivation behind iterators is twofold. Working with data
as sequences is often the most straightforward approach, and a
sequence that is processed in linear order often does not need to
actually exist all at once.

“The x*() premonitions provide clear examples of these
principles. If you want to do something a billion times, your
program will probably take a while to execute, but there is no
general need for it to claim a lot of memory. Likewise, for many
types of files, processing can be a line-by-line matter, and there
is no need to store the whole file in memory. All sorts of other
sequences can best be treated lazily too; they might depend on data
arriving incrementally over a channel, or upon a computation that
proceeds step-by-step…”


Complete Story

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends, & analysis