//__new__(cls[,....])
//对象实例化调用的第一个方法,它的第一个参数是这个类,而其他参数会直接传递给__init__()方法
//需要在基类的基础上对其进行修改时重写__new__()方法
![]()
//__del__()方法
//只有在该类实例化的对象全部被del掉时,才调用__del__()方法
![]()
//python 中的运算符重载
class New_int(int): //基于基类int的子类New_int
def__add__(self,other) //重载运算符 +
return int.__sub__(self,other) //返回基类的减法运算
class New_Int(int):
def__add__(self,other):
return int(self)-int(other) // 等价于int. __sub__(self,other)
>>>a = New_Int(1)
>>>b = New_int(3)
//反运算
//左操作数不支持相应的操作时右操作数的反运算方法会被调用。
>>> class Newint(int):
def __add__(self,other):
print("add 被调用")
def __radd__(other,self):
print("radd 被调用")
>>> a = Newint(5)
>>> b= Newint(6)
>>> a + b
add 被调用
>>> 1+a
radd 被调用