[Python-Dev] Termination of two-arg iter()

Guido van Rossum guido@python.org
Sat, 13 Jul 2002 19:07:36 -0400


> [Tim]
> >> The base question:  does the iteration protocol define what
> >> happens if an iterator's next() method is called after the iterator
> >> has raised StopIteration?  Or is that left up to the discretion of the
> >> iterator?
> 
> [Guido]
> > The latter.  Believe it or not, I thought about this during the design
> > of the protocol, and decided that if someone wanted to create an
> > iterator that could somehow continue after raising StopIteration, that
> > should be their problem.

[Tim]
> I believe it -- I even vaguely recall discussions about it.  Unfortunately,
> they don't seem to be recorded anywhere I can find now.
> 
> > Basically, the effect of calling next() after StopIteration is raised is
> > undefined.
> 
> That's consistent with a lawyer's reading of the relevant PEP <wink>.

Actually, not.  Under "Resolved Issues" the PEP has this:

    - Once a particular iterator object has raised StopIteration, will
      it also raise StopIteration on all subsequent next() calls?
      Some say that it would be useful to require this, others say
      that it is useful to leave this open to individual iterators.
      Note that this may require an additional state bit for some
      iterator implementations (e.g. function-wrapping iterators).

      Resolution: once StopIteration is raised, calling it.next()
      continues to raise StopIteration.

So I misremembered, and Tim didn't read the PEP closely enough. :-)

> I'm happy to leave this be: the docs match the implemenation, I'm
> sure *someone* relies on that by now, and the behavior is easy to
> explain as-is.

Hm.  Given what the PEP says, I'm ready to have this fixed (even in
2.2.2).  I can't call code relying on this sane.

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