[Python-Dev] Please comment on PEP 357 -- adding nb_index slot to PyNumberMethods

Armin Rigo arigo at tunes.org
Fri Feb 17 17:29:32 CET 2006

Hi Travis,

On Tue, Feb 14, 2006 at 08:41:19PM -0700, Travis E. Oliphant wrote:
>     2) The __index__ special method will have the signature
>        def __index__(self):
>            return obj
>        Where obj must be either an int or a long or another object that has the
>        __index__ special method (but not self).

The "anything but not self" rule is not consistent with any other
special method's behavior.  IMHO we should just do the same as

* __nonzero__(x) must return exactly a bool or an int.

This ensures that there is no infinite loop in C created by a
__nonzero__ that returns something that has a further __nonzero__

The rule that the PEP proposes for __index__ (returns anything but not
'self') is not useful, because you can still get infinite loops (you
just have to work slightly harder, and even not much).  We should just
say that __index__ must return an int or a long.

A bientot,


More information about the Python-Dev mailing list