算术运算的问题
>>> class Nint(int):def __rsub__(self,other):
return int.__sub__(self,other)
>>> a = Nint(4)
>>> b = Nint(6)
>>> c = Nint(50)
>>> 3-b-a
1
>>> class Nint(int):
def __rsub__(self,other):
return int.__sub__(other,self)
>>> 3-b-a
1
>>>
小甲鱼说这里的self是b的对象 other是a的对象,但是这么更改顺序也没有问题啊 这样是没问题,但是换成这样就有问题了:
>>> class Nint(int):
def __rsub__(self, other):
return int(self) - int(other)
>>> a = Nint(3)
>>> 5 - a # 变成了 a - 5
-2 {:5_99:}
页:
[1]