对c+d和c-d的结果有疑问,代码执行过程中参数是如何传递的
class New_int(int):
def __add__(self,other):
return int.__sub__(self,other)
def __sub__(self,other):
return int.__add__(self,other)
c = New_int(3)
c
3
d = New_int(5)
d
5
c + d
-2
-2
-2
c - d
8
c+d
有+号运算符,会进入New_int中执行__add__(self,other)c传入self,d传入other,而__add__函数体内又改变了计算规则,调用了父类中的__sub__,所以实际是进行了减法运算
c-d同上