算术运算重写:对已经定义的函数进行重写
>> 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(2)
>>> b=New_int(9)
>>> a+b
-7
>>> a-b
11
>>>
__add__(self, other)
|
定义加法的行为:
+
|
__sub__(self, other)
|
定义减法的行为:
-
|
__mul__(self, other)
|
定义乘法的行为:
*
|
__truediv__(self, other)
|
定义真除法的行为:
|