Stephan Hoyer wrote:
In practice, CPython requires that the right operand defines a different method before it defers to it.
I'm not sure exactly what the rationale for this behaviour is, but it's probably something along the lines that the left method should already know how to deal with that combination of types, and right methods are only supposed to be called as a fallback if the left method can't handle the operands, so calling it in that situation would be wrong. Following that logic, the wrapper's __add__ method in your example needs to allow for the subclassing case, e.g. def __add__(self, other): t1 = type(self) t2 = type(other) t = t2 if issubclass(t2, t1) else t1 return t(self.value + other.value)
the behavior is different for comparisons, which defer to subclasses regardless of whether they implement a new method
Comparisons are a bit different, because they don't have separate left and right methods, although it's hard to see exactly how that affects the logic.
I think this behavior is a mistake and should be corrected.
Possibly, but it's not clear how much breakage might result from changing it now. Although...
The 3.x docs don't have the "and overrides" language;
so arguably we would be changing the implementation to match the docs. -- Greg _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/