[Python-Dev] Single- vs. Multi-pass iterability

Tim Peters tim.one@comcast.net
Mon, 08 Jul 2002 14:44:25 -0400


[David Abrahams]
> I keep running into the problem that there is no reliable way to
> introspect about whether a type supports multi-pass iterability (in the
> sense that an input stream might support only a single pass, but a list
> supports multiple passes). I suppose you could check for __getitem__, but
> that wouldn't cover linked lists, for example.
>
> Can anyone channel Guido's intent for me? Is this an oversight or a
> deliberate design decision? Is there an interface for checking
> multi-pass-ability that I've missed?

The language makes no such distinctions.  If an app wants to make them, it's
up to the app to implement them.  Likewise for a way to tell a multipass
iterator to "start over again".  The Python iteration protocol has only two
methods, .next() to get "the next" item, and .iter() to return self; given a
random iterator, those are the only things you can rely on.