Issue 1462278: Coercion rules incomplete in Reference Manual (original) (raw)

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.