[Python-ideas] Is there a reason some of the PyLong_As* functions don't call an object's __int__?

Ethan Furman ethan at stoneleaf.us
Fri Dec 8 13:20:44 EST 2017


On 12/08/2017 04:33 AM, Erik Bray wrote:

> More importantly not as many objects that coerce to int actually
> implement __index__.  They probably *should* but there seems to be
> some confusion about how that's to be used.

__int__ is for coercion (float, fraction, etc)

__index__ is for true integers

Note that if __index__ is defined, __int__ should also be defined, and return the same value.

https://docs.python.org/3/reference/datamodel.html#object.__index__

--
~Ethan~


More information about the Python-ideas mailing list