Operator commutativity
Mark Dickinson
dickinsm at gmail.com
Wed Sep 21 13:03:15 EDT 2011
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?
--
Mark
More information about the Python-list
mailing list