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

Guido van Rossum guido@python.org
Wed, 17 Jul 2002 20:43:20 -0400


> > This is a very good summary of the two iterator protocols.  Ping,
> > would you mind adding this to PEP 234?
> 
> And i thought it was a critique.  Fascinating, Captain. :)
> 
> I'm happy to add the text, but i want to be clear, then: is it
> acceptable to write an iterator that only provides <next> if you
> only care about the "iteration protocol" and not the "for-able
> protocol"?

No, an iterator ought to provide both, but it's good to recognize that
there *are* two protocols.

> I see that "ought to" is the most opinion the PEP is willing to
> give on the topic:
> 
>     A class is a valid iterator object when it defines a next()
>     method that behaves as described above.  A class that wants
>     to be an iterator also ought to implement __iter__()
>     returning itself.

I would like to see this strengthened.  I envision "iterator algebra"
code that really needs to be able to do a for loop over an iterator
when it feels like it.

--Guido van Rossum (home page: http://www.python.org/~guido/)