这是一个随手写的随机运算的小程序。
加减乘均正常,在随机到3返回除法时会直接报错 type object 'int' has no attribute '__div__'。
属实是没想明白这是什么毛病,第一次见整形不能除这个错误
import random
test = random.randint(0,3)
class Magic(int):
def __add__(self,other):
if test == 0:
return int.__add__(self,other)
elif test == 1:
return int.__sub__(self,other)
elif test == 2:
return int.__mul__(self,other)
elif test == 3:
return int.__div__(self,other)
a = Magic(3)
b = Magic(5)
print(a + b)