On Thu, Oct 14, 2021 at 10:51 AM Victor Stinner <vstin...@python.org> wrote: > > Honestly, I don't understand well the difference between __int__() and > __index__(). > > * https://docs.python.org/dev/reference/datamodel.html#object.__int__ > * https://docs.python.org/dev/reference/datamodel.html#object.__index__
__int__ is for converting to integer, __index__ is for interpreting as integer. The intention of __index__ is that it already has that exact value, whereas __int__ might be rounding. For instance, int(5.25) is 5, but operator.index(5.25) raises. Quoting from that linked page on __index__: "Called ... whenever Python needs to **losslessly** convert the numeric object to an integer object" It is assumed to already be a numeric type (so "5" isn't, even though it could be cast to int), and the conversion should be lossless. I'm not 100% sure, but I think that, if __index__ returns anything, __int__ should return the same thing. There could be edge cases where that's not true though. ChrisA _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/WZSTHNTNZWHIKXWK2OOQK3MYI6PGPBJ5/ Code of Conduct: http://python.org/psf/codeofconduct/