[Python-Dev] int() and math.trunc don't accept objects that only define __index__

Nick Coghlan ncoghlan at gmail.com
Tue Feb 19 08:39:54 EST 2019


On Tue, 19 Feb 2019 at 03:31, Rémi Lapeyre <remi.lapeyre at henki.fr> wrote:
> Nick Coghlan proposes to make __int__ defaults to __index__ when only the second
> is defined and asked to open a discussion on python-dev before making any change
> "as the closest equivalent we have to this right now is the "negative" derivation,
> where overriding __eq__ without overriding __hash__ implicitly marks the derived
> class as unhashable (look for "type->tp_hash = PyObject_HashNotImplemented;").".

Reading this again now, it occurs to me that there's another developer
experience improvement we already made along these lines in Python 3:
"By default, __ne__() delegates to __eq__() and inverts the result
unless it is NotImplemented. " [1]

By contrast, the corresponding (and annoying) Python 2 behaviour was:
"The truth of x==y does not imply that x!=y is false. Accordingly,
when defining __eq__(), one should also define __ne__() so that the
operators will behave as expected." [2]

The only difference is that whereas the new `__ne__` delegation
behaviour could just be defined directly in `object.__ne__()`,
`object` doesn't implement `__int__` by default, so the delegating
function would need to be injected into the type when it is defined
(and that's the part that's similar to the `__hash__ = None` negative
derivation).

So +1 from me.

Cheers,
Nick.

[1] https://docs.python.org/3/reference/datamodel.html#object.__ne__
[2] https://docs.python.org/2/reference/datamodel.html#object.__ne__

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list