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? |