这里是因为无限调用重写的加法递归导致出错
class newint(int):def __add__(self,other):
return self+other
def __sub__(self,other):
return self-other
这里是因为无限调用重写的加法递归导致出错为什么Python自己的int里面的加法不会无限递归?他是怎么实现加法的? 不会。用C语言实现的 class newint(int):
def __init__(self, num):
self.num = int(num)
super().__init__()
def __add__(self,other):
s = self.num ^ other
carry = (self.num & other) << 1
while carry != 0 :
a = s
b = carry
s = a ^ b
carry = (a & b) << 1
return s
a = newint(11)
b = newint(12)
print(a+b)
网上找的代码改的
参考https://blog.csdn.net/YPJMFC/article/details/78246971
页:
[1]