[Python-Dev] why not "return StopIteration"?

Neil Schemenauer nas@python.ca
Fri, 22 Jun 2001 07:43:17 -0700


Is "raise StopIteration" an abuse of exceptions?  Why can we not
use "return StopIteration" to signal the end of an iterator?
I've done a bit of hacking and the idea seems to work.

On possible problem is that the StopIteration object in the
builtin module could cause some confusing behavior.  For example
the code:

    for obj in __builtin__.__dict__.values():
        print obj

would not work as expected.  This could be fixed in most causes
by changing the tp_iternext protocol.  Something like:

    int tp_iternext(PyObject *it, PyObject **item)

were the return value is 1, 0, or -1.  IOW, StopIteration would not
have to come into the protocol if the object implemented
tp_iternext.

  Neil