The eighth item in that list says:
Exception to the previous item: if the left operand is
an instance of a built-in type or a new-style class,
and the right operand is an instance of a proper
subclass of that type or class, the right operand's
__rop__() method is tried before the left operand's
__op__() method. This is done so that a subclass can
completely override binary operators. Otherwise, the
left operand's __op__ method would always accept the
right operand: when an instance of a given class is
expected, an instance of a subclass of that class is
always acceptable.
This is not correct; subclass's __rop__() method is
called only if it has overloaded the base class's
method; example::
class A(object):
def __add__(self, other):
return self.__class__.__name__
__radd__ = __add__
class B(A):pass
a = A()
b = B()
print b + a # prints B
print a + b # prints A
According to the docs B should be printed in both
cases. The change in behaviour was introduced in
revision 30639.
|