[Python-Dev] operator precedence of eq, ne, etc, if both object have implementations (original) (raw)

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