[issue20092] type() constructor should bind __int__ to __index__ when __index__ is defined and __int__ is not
Patrick Yang
report at bugs.python.org
Fri Nov 19 08:14:29 EST 2021
Patrick Yang <patrick.yang.1248 at gmail.com> added the comment:
I ended up in this issue after I learnt the following from the Python Library Reference Manual.
----
float(..).
For a general Python object x, float(x) delegates to x.__float__(). If __float__() is not defined then it falls back to __index__().
----
The discussion on __int__() and __index__() was very interesting but I still didn't get the answer I wanted.
If __int__() is assumed to be a possibly approximate conversion and it's possible that __int__() may exist while __index__() doesn't, shouldn't __int__() be used as a fall back before __index__()?
The downside would be that the resulting float may not be "very close" to the original object because __int__() is only an approximation while __index__() guarantees exact, but loss of precision is acceptable during type conversion, isn't it? (i.e. int(3.14) -> 3). Perhaps it's not acceptable if the conversion is a widening conversion and that's why __int__() is skipped?
----------
nosy: +patrick.yang.1248
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue20092>
_______________________________________
More information about the Python-bugs-list
mailing list