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

David Abrahams David Abrahams" <david.abrahams@rcn.com
Tue, 9 Jul 2002 07:53:03 -0400


From: "Oren Tirosh" <oren-py-d@hishome.net>


> On Tue, Jul 09, 2002 at 04:43:25AM -0400, David Abrahams wrote:
> > Yep. [Part of the reason I want to know whether I've got a one-shot
> > sequence is that inspecting that sequence then becomes an
> > information-destroying operation -- only being able to touch it once
> > changes how you have to handle it]
> >
> > I was thinking one potentially nice way to introspect about
> > multi-pass-ability might be to get an iterator and to see whether it
was
> > copyable. Currently even most multi-pass iterators can't be copied with
> > copy.copy().
>
> I wouldn't call it a one-shot sequence - it's just an iterator.  The name
> iterator is enough to suggest that it is disposable and good for just one
> pass through the container.
>
> If the object has an __iter__ method but no next it's not an iterator and
> therefore most likely re-iterable.  One notable exception is a file
object.
> File iterators affect the current position of the file.

No kidding, that's the problem I'm talking about. It does me no good to
have a criterion for determinining re-iterability which fails for the case
I'm most concerned with ;-)

> If you think about
> it you'll see that file objects aren't really containers - they are
already
> iterators.  The real container is the file on the disk

There might not be a "real container" -- if it's an input pipe the data
disappears as you iterate it.

-Dave