class New_int(int): #定义一个类,继承于int
def __add__(self,other):
return int.__sub__(self,other) #将加法定义为减法
def __sub__(self,other):
return int.__add__(self,other) #将减法定义为加法
a = New_int(3);
b = New_int(5); #定义a,b
我不懂的地方就是为什么当a+b引用“+”时,会调用
def __add__(self,other):
return int.__sub__(self,other)
但是int.__sub__(self,other) 已经被定义,然后继续调用__add__(self,other),然后__add__(self,other)又会调用int.__sub__(self,other) ,这会产生迭代,但是在Python结果运行当中,并没有出现这种情况,这是为什么