运算符重载调用的方法:
Method Overloads Call for
__init__ 构造函数 X=Class()
__del__ 析构函数 对象销毁
__add__ + X+Y,X+=Y
__or__ | X|Y,X|=Y
__repr__ 打印转换 print X,repr(X)
__str__ 打印转换 print X,str(X)
__call__ 调用函数 X()
__getattr_ 限制 X.undefine
__setattr__ 取值 X.any=value
__getitem__ 索引 X[key],
For If
__len__ 长度 len(X)
__cmp__ 比较 X==Y,X<Y
__lt__ 小于 X<Y
__eq__ 等于 X=Y
__radd__ Right-Side + +X
__iadd__ += X+=Y
__iter__ 迭代 For In
减法重载:
class Number:
def __init__(self, start):
self.data = start
print "self.data = ", self.data
def __sub__(self, other):
print "other = ", other
return Number(self.data - other)
number = Number(20)
y = number - 10
print "y.data = ", y.data
输出:
self.data = 20
other = 10
self.data = 10
y.data = 10
...