[Python-Dev] The iterator story

Guido van Rossum guido@python.org
Sat, 20 Jul 2002 10:13:34 -0400


> > >     Based on Guido's positive response, in which he asked me to make
> > >     an addition to the PEP, i believe Guido agrees with me that
> > >     __iter__ is distinct from the protocol of an iterator.  This
> > >     surprised me because it runs counter to the philosophy previously
> > >     expressed in the PEP.
> > 
> > I recognize that they are separate protocols.  But because I like the
> > for-loop as a convenient way to get all of the elements of an
> > iterator, I want iterators to support __iter__.
> 
> Is this the only reason iterators are required to support __iter__?

Yes.

> It seems like a strange design decision to put the burden on all iterator 
> implementers to write a dummy method returning self instead of just checking 
> if tp_iter==NULL in PyObject_GetIter. It's like requiring all class writers 
> to write a dummy __str__ method that calls __repr__ instead of implementing 
> the automatic fallback to __repr__ in PyObject_Str when no __str__ is 
> available.

I suppose you meant "check for tp_iter==NULL and tp_iternext!=NULL.

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