[Python-Dev] iterator API in Py3.0

Michael Chermside mcherm at mcherm.com
Fri Mar 3 22:06:50 CET 2006

Raymond writes:
> The double underscore convention is appropriate where the method is always
> invoked magically in normal code and not called directly.  The next() method
> differenct because it is a mixed case, sometimes called magically and
> called directly.  In the latter case, the name is highly visible and therefore
> should not have double underscores.

I think it's clear that if a method is invoked magically, it ought to have
underscores; if it is invoked directly then it ought not to. next() is
invoked both ways, so the question is which of the following invariants
we would rather maintain:

  * Any method that gets invoked 'magically' (by syntax) will have

  * Any method with underscores should be invoked directly only if
    you are practicing deep magic.

What I would *really* like is to keep both invariants... but the only way
to do that is to have a __next__() method paired with a next() builtin.
(The pattern we use for things like len().) This is great for everyone
except for (1) backward compatibility, (2) performance (but does the
indirection really need to hurt, and how much?), and (3) proliferation
of builtins (adding one more for next() won't hurt, but if we make a
practice of it we'll eventually have too many).

All told, I prefer using the underscores. I think that the first
invariant is important. But it's a judgement call, and a close one, so
I'll be happy with either decision.

-- Michael Chermside

More information about the Python-Dev mailing list