Operator commutativity

Ethan Furman ethan at stoneleaf.us
Wed Sep 21 20:15:56 CEST 2011


Mark Dickinson wrote:
> On Sep 21, 2:07 am, Steven D'Aprano <steve
> +comp.lang.pyt... at pearwood.info> wrote:
>> After playing around with various combinations of C1, C2, D1 and D2, it
>> seems to me that the rule is:
>>
>> If the right-hand argument is a subclass of the left-hand argument, AND also
>> defines __radd__ directly rather than inheriting it, then its __radd__
>> method is called before the left-hand argument's __add__ method.
>>
>> which strikes me as a strangely specific and not very useful rule. I suspect
>> it might be an accident of implementation rather than a deliberate feature.
> 
> I'm 99.9% sure it's deliberate rather than an accident of
> implementation.  See the note in the docs at:
> 
> http://docs.python.org/reference/datamodel.html#emulating-numeric-types
> 
> Support that you're subclassing int, (class MyInt(int): ...) and you
> want to intercept additions of the form 3 + MyInt(4)  (perhaps because
> you want MyInt to be 'contagious', so that an arithmetic operation
> that combines an int and a MyInt returns a MyInt).  How would you
> achieve this without this rule?

I think Steven's objection was in not calling subclass.__radd__ when 
__radd__ is inherited rather than directly overridden.  Interestingly 
enough, the following works as expected (at least, as I expected ;)...

class C1(object):
      def __add__(self, other):
          print "C1.__add__(%r, %r)" % (self, other)

class C2(C1):
      def __radd__(self, other):
          print "C2.__radd__(%r, %r)" % (self, other)

class C3(C2):
     pass

C1() + C2()  # --> C2.__radd__(<...C2...>, <...C1...)
C1() + C3()  # --> C2.__radd__(<...C3...>, <...C1...>)
C2() + C3()  # --> C1.__add__(<...C2...>, <...C3...>)


~Ethan~



More information about the Python-list mailing list