[Python-Dev] Semantics of __int__(), __index__()

Ethan Furman ethan at stoneleaf.us
Wed Apr 3 17:21:22 CEST 2013

On 04/03/2013 08:14 AM, Nick Coghlan wrote:
> On 4 Apr 2013 00:18, "Barry Warsaw" <barry at python.org <mailto:barry at python.org>> wrote:
>> __index__() is a bit trickier because it is not tied directly to type
>> conversion.  In this case, int subclasses could be valid, and as Hrvoje later
>> points out, returning int-subclasses from __index__() should still work for
>> all valid use cases.
> Implementing __index__ just means "This type can be converted to a Python integer without losing information". Aside
> from that extra "without information loss" qualification, it's the same as __int__.

How is that possible?  Whether int or int subclass, if I'm implementing __index__ it means my type is not an int 
subclass, and when I return an int I most certainly have lost information from the original type.


More information about the Python-Dev mailing list