[Python-Dev] operator precedence of __eq__, __ne__, etc, if both object have implementations

Guido van Rossum guido at python.org
Thu Sep 24 04:02:43 CEST 2009


On Wed, Sep 23, 2009 at 5:44 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Nick Coghlan wrote:
>
>> The interpreter doesn't promise to call those slots with "self" first -
>> self will be the second argument in the "rop" case.
>
> I know. My question is: How does it know whether a subclass
> "has overridden __rop__" when there is no concept of an
> __rop__ method distinct from the __op__ method?

This is a constraint on types implemented in C -- the same definition
is required to apply to __op__ and __rop__.

The datetime module actually has a slight problem here because it
wants to override datetime - timedelta but not timedelta - datetime
(since the latter would have to return a negative time, which is
meaningless). It solves this easily by doing a type check on each
argument and returning Py_NotImplemented for the unimplemented
combinations -- see the various _subtract functions in
Modules/datetimemodule.c. Returning NotImplemented is close enough to
not actually implementing the method.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list