方法 |
重载 |
调用 |
__init__ | 构造函数 | x = Class() |
__del__ | 析构函数 | del x |
__str__ | 打印 | print x |
__call__ | 调用函数 | x(*args) |
__getattr__ | 获取属性 | y = x.method |
__setattr__ | 设置属性 | x.any = value |
__getitem__ | 获取索引 | x[key] |
__setitem__ | 设置新索引 | x[key] = value |
__len__ | 长度 | len(x) |
__iter__ | 迭代 | for item in x: |
__add__ | 加 | x + y |
__sub__ | 减 | x - y |
__mul__ | 乘 | x * y |
__radd__ | ||
__iadd__ | 左加 += | x += y |
__or__ | 或 | | x | y |
__cmp__ | 比较 == | x == y |
__lt__ | 小于 < | x < y |
__eq__ | 等于 = | x = y |
减法重载
重载"-" 不同对象的减法处理
class Number:
def __init__(self,start):
self.data=start
def __sub__(self,other):
return Number(self.data-other)
number=Number(20)
y=number-10
print number.data, y.data
重载"-" 相同对象的减法处理
class Big :
def __init__(self,a,b):
self.a=a
self.b=b
def __sub__(self,other):#other不用事先制定类型,可以直接当做一个Big来用
return Big(self.a-other.a,self.b-other.b)
i=Big(20,12);
j=Big(23,4);
k=i-j;
print k.a ,k.b
重载"+"
class AddOperator :
def __init__(self,a,b):
self.a=a
self.b=b
def __add__(self,other):#other不用事先制定类型,可以直接当做一个Big来用
return Big(self.a+other.a,self.b+other.b)
i=AddOperator(20,12);
j=AddOperator(23,4);
k=i+j;
print k.a ,k.b
重载"+="
class AddOperator :
def __init__(self,a,b):
self.a=a
self.b=b
def __iadd__(self,other):#other不用事先制定类型,可以直接当做一个Big来用
return Big(self.a+other.a,self.b+other.b)
i=AddOperator(20,12);
j=AddOperator(23,4);
i+=j;
print i.a ,i.b
重载乘法
不同对象的乘法:
class MulOperator:
def __init__(self,a,b):
self.a=a
self.b=b
def __mul__(self,n):
return MulOperator(self.a*n,self.b*n)
i=MulOperator(20,5)
j=i*4.5
print j.a,j.b
索引重载
class indexer:
def __getitem__(self, index): #iter override
return index ** 2
X = indexer()
X[2]
for i in range(5):
print X[i] class indexer:
def __setitem__(self, key,value): #iter override
self.mydict[key] = value
return self.mydict
X = indexer() X[2] = 'test' # 它等于调用 X.__setitem__(2, 'test')
打印重载
class adder :
def __init__(self,value=0):
self.data=value
def __add__(self,other):
self.data+=other
class addrepr(adder):
def __repr__(self):
return "addrepr(%d)"% self.data # %d ,%s都可以
x=addrepr(2)
x+1
print x ,repr(x)
i=3;
print "%s---%d"% (i, i)
调用重载
__call__相当与 X()
class Prod:
def __init__(self, value):
self.value = value
def __call__(self, other):
return self.value * other p = Prod(2) # call __init__
print p(1) # call __call__
print p(2)
析构重载 __del__
class Life:
def __init__(self, name='name'):
print 'Hello', name
self.name = name
def __del__(self):
print 'Goodby', self.name brain = Life('Brain') # call __init__
brain = 'loretta' # call __del__
重载"|"
class Mat :
def __init__(self,value):
self.age=value
def __or__(self,other):
return self.age!=0 and other.age!=0
a=Mat(10)
b=Mat(21)
c=Mat(0)
print a|b ,a|c
打印转换重载
class PrintOperator:
def __init__(self,a):
self.a=a
def __str__(self,b=50):
return "I am %s years old!" % self.a
i=PrintOperator(10)
print i, str(i)
长度重载
class lenOperator:
def __init__(self,a,b,c):
(self.a,self.b,self.c)=(a,b,c)
def __len__(self):
return 3
a=lenOperator(0,2,4)
print len(a)
cmp重载
class cmpOperator:
def __init__(self,a,b,c):
(self.a,self.b,self.c)=(a,b,c)
def __cmp__(self,other):
if self.a>other.a:
return 1
elif self.a<other.a:
return -1
elif self.b>other.b:
return 1
elif self.b<other.b:
return -1
elif self.c>self.c:
return 1
elif self.c<self.c:
return -1
elif self.c==self.c:
return 0
i=cmpOperator(1,2,3)
j=cmpOperator(2,4,5)
k=cmpOperator(2,4,5)
a=cmpOperator(1,4,5)
print cmp(i,j),cmp(j,k),cmp(a,i)
delattr重载
class delattrOperator(object):
def __init__(self,a,b):
(self.a,self.b)=(a,b)
def __delattr__(self,name):
print "del obj.%s" % name
object.__delattr__(self,name)
a=delattrOperator(1,2)
print a.a,a.b
del a.a
print a.b
# print a.a 打印a会出错,a已经被删除。
getAttr/setAttr重载
class empty:
def __getattr__(self,attrname):
if attrname == 'age':
return 40
else:
raise AttributeError,attrname
X = empty()
print X.age # call__getattr__ class accesscontrol:
def __setattr__(self, attr, value):
if attr == 'age':
# Self.attrname = value loops!
self.__dict__[attr] = value
else:
print attr
raise AttributeError, attr + 'not allowed' X = accesscontrol()
X.age = 40 # call __setattr__
X.name = 'wang' # raise exception