dcsimg
Linux Today: Linux News On Internet Time.





More on LinuxToday


developerWorks: Using Combinatorial Functions in the itertools Module

Jul 14, 2003, 07:00 (0 Talkback[s])
(Other stories by David Mertz)

WEBINAR:
On-Demand

How to Help Your Business Become an AI Early Adopter


"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

Related Stories: