[Python-ideas] __iter__ implies __contains__?

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Oct 4 08:30:32 CEST 2011


Raymond Hettinger wrote:

> Really?   Passing around iterators is a basic design pattern
> http://en.wikipedia.org/wiki/Iterator_pattern for lots of languages.

I'm not suggesting that we stop using iterators altogether,
only that reiterables are often preferable when there's
a choice. Passing a reiterable to a piece of third-party
library code is safer and more future-proof than passing an
iterator, because it makes less assumptions about what will
be done to it.

There are certainly some objects that are inherently
non-reiterable, such as file objects reading from pipes
or sockets, but there are many others that *are* reiterable.
Some of them, such as itertools.count(), are currently
only available as iterators, but could just as easily
be made available in a reiterable version. And the
deiter() function posted earlier shows that it's always
possible to construct a reiterable analogue of any
iterator-algebra operator, provided you have reiterable
base objects to work with.

> You may have a personal programming style that avoids
> iterators, but that shouldn't be forced on the rest of the 
 > community.

I don't mean to force it, but to make it at least as easy
to use a reiterable-based style as an iterator-based one
wherever it's reasonably possible. Ideally, reiterables
should be the most obvious (in the Dutch sense) choice,
with iterators being the next-most-obvious choice for when
you can't use reiterables.

> The only way for a broad categories of iterators to become
> reiterable is for their outputs to be stored in memory

I'm not sure the category is as broad as all that. Note that
it does *not* include infinite iterables whose elements are
generated by an algorithm, such as itertools.count(). It
doesn't even include disk files, however large they might
be, since you can in principle open another stream reading
from the same file (although the traditional way of manifesting
files as objects doesn't make that as straightforward as it
could be).

-- 
Greg



More information about the Python-ideas mailing list