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

Antoine Pitrou solipsis at pitrou.net
Wed Apr 3 17:41:59 CEST 2013


Le Wed, 03 Apr 2013 08:21:22 -0700,
Ethan Furman <ethan at stoneleaf.us> a écrit :
> 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.

Without losing information about the numeric value.

Regards

Antoine.




More information about the Python-Dev mailing list