Operator commutativity

Ethan Furman ethan at stoneleaf.us
Tue Sep 20 15:09:15 EDT 2011


Peter Pearson wrote:
> On Mon, 19 Sep 2011 05:48:07 -0700, Ethan Furman <ethan at stoneleaf.us> wrote:
> [snip]
>> Also, if the right-hand operand is a subclass of the left-hand operand 
>> then Python will try right-hand_operand.__radd__ first.
> 
> I don't think it works that way for me:
> 
> Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
>>>> class C1():
> ...     def __add__(self, other):
> ...         print "C1.__add__()"
> ... 
>>>> class C2(C1):
> ...     def __radd__(self, other):
> ...         print "C2.__radd__()"
> ... 
>>>> C1() + C2()
> C1.__add__()

Oh, it has to be a new-style class.  Sorry.

~Ethan~




More information about the Python-list mailing list