Operator commutativity

Chris Angelico rosuav at gmail.com
Tue Sep 20 21:31:57 EDT 2011

On Wed, Sep 21, 2011 at 11:07 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> 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.

It makes sense, but in a weird way. (Maybe I understand it because I'm
half Dutch? heh) It means that a subclass can override addition for
itself - presumably, it'll define __add__ and __radd__ both - and that
the only time you'd get a false positive (where a function thinks it
can handle the addition but actually there's a better one) is when
it's a subclass. So this is probably correct behaviour, but it's a
fairly weird and esoteric rule.


More information about the Python-list mailing list