如果右操作數(shù)的類型是左操作數(shù)類型的子類,并且該子類提供了操作的反射方法,則該方法將在左操作數(shù)的非反射方法之前被調(diào)用。
If the right operand’s type is a subclass of the left operand’s type and that subclass provides the reflected method for the operation, this method will be called before the left operand’s non-reflected method. This behavior allows subclasses to override their ancestors’ operations.
這樣就解釋了左右操作的不同,之前自己老是想著自定義的優(yōu)先
>>> class Nint(int):
? ? ? ? def __radd__(self,other):
? ? ? ? ? ? ? ? return int.__sub__(other,self)
>>> a=Nint(5)
>>> b=Nint(3)
>>> 1+b
-2
>>> b+1
4