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

Serhiy Storchaka storchaka at gmail.com
Fri Dec 8 07:30:00 EST 2017


08.12.17 13:36, Antoine Pitrou пише:
> On Fri, 8 Dec 2017 13:26:48 +0200
> Serhiy Storchaka <storchaka at gmail.com>
> wrote:
>>
>>> Currently the following functions fall back on __int__ where available:
>>>
>>> PyLong_AsLong
>>> PyLong_AsLongAndOverflow
>>> PyLong_AsLongLong
>>> PyLong_AsLongLongAndOverflow
>>> PyLong_AsUnsignedLongMask
>>> PyLong_AsUnsignedLongLongMask
>>
>> I think this should be deprecated (and there should be an open issue for
>> this). Calling __int__ is just a Python 2 legacy.
> 
> I think that's a bad idea.  There are widely-used int-like classes out
> there and it will break actual code:
> 
>>>> import numpy as np
>>>> x = np.int64(5)
>>>> isinstance(x, int)
> False
>>>> x.__int__()
> 5

NumPy integers implement __index__.



More information about the Python-ideas mailing list