[Python]重载运算符
来源:互联网 发布:中文安卓编程 编辑:程序博客网 时间:2024/06/05 16:49
Python运算符重载
MethodOverloadsCall for__init__构造函数X=Class()__del__析构函数对象销毁__repr__打印转换print X,repr(X)__str__打印转换print X,str(X)__call__调用函数X()__getattr_限制X.undefine__setattr__取值X.any=value__getitem__索引X[key],For If__setitem__索引X[key]=value__len__长度len(X)__iter__迭代For In
__add__+X+Y,X+=Y__sub__-X-Y,X-=Y__mul__*X*Y__radd__右加++X__iadd__+=X+=Y__or__|X|Y,X|=Y__cmp__比较 ==X==Y,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-10print 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.5print j.a,j.b
重载乘法:两个对象的相乘似乎也有用,如矩阵的相乘,3D中几个变换矩阵的相乘。
两个4*4矩阵相乘
矩阵 的列数必须等于矩阵 的行数,矩阵 与矩阵 才能相乘,第一个矩阵由4个横向排列的四维向量组成,第二个矩阵由4个纵向排列的四维向量组成,
class Vector4: def __init__(self,a,b,c,d): (self.a1,self.a2,self.a3,self.a4)=(a,b,c,d) def __mul__(self,other): return (self.a1*other.a1+self.a2*other.a2+self.a3*other.a3+self.a4*other.a4) def PrintVector(self): print self.a1,self.a2,self.a3,self.a4class Matrix : def __init__(self,a,b,c,d): (self.a1,self.a2,self.a3,self.a4)=(a,b,c,d) #(self.a1,self.a2,self.a3,self.a4)=(a,b,c,d) def __mul__(self,other): x1=Vector4(other.a1.a1,other.a2.a1,other.a3.a1,other.a4.a1) x2=Vector4(other.a1.a2,other.a2.a2,other.a3.a2,other.a4.a2) x3=Vector4(other.a1.a3,other.a2.a3,other.a3.a3,other.a4.a3) x4=Vector4(other.a1.a4,other.a2.a4,other.a3.a4,other.a4.a4) a=Vector4(self.a1*x1,self.a1*x2,self.a1*x3,self.a1*x4) b=Vector4(self.a2*x1,self.a2*x2,self.a2*x3,self.a2*x4) c=Vector4(self.a3*x1,self.a3*x2,self.a3*x3,self.a3*x4) d=Vector4(self.a4*x1,self.a4*x2,self.a4*x3,self.a4*x4) return Matrix(a,b,c,d) def PrintMatrix(self): self.a1.PrintVector() self.a2.PrintVector() self.a3.PrintVector() self.a4.PrintVector()i1=Vector4(1,4,1,4)i2=Vector4(2,1,6,7)k=i1*i2#测试向量点乘print ki3=Vector4(1,1,1,1)i4=Vector4(1,2,4,5)j1=Vector4(1,2,3,4)j2=Vector4(2,1,1,1)j3=Vector4(1,3,21,2)j4=Vector4(2,4,3,7)a=Matrix(i1,i2,i3,i4)b=Matrix(j1,j2,j3,j4)c=a*bc.PrintMatrix()
索引重载
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+=otherclass addrepr(adder): def __repr__(self): return "addrepr(%d)"% self.data #%d ,%s都可以 x=addrepr(2)x+1print 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!=0a=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.ai=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 3a=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.bdel a.aprint 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
阅读全文
0 0
- Python运算符重载
- Python运算符重载
- Python运算符重载
- Python运算符重载
- Python运算符重载
- python 重载运算符
- Python运算符重载
- python重载运算符
- python运算符重载
- Python 运算符重载
- Python运算符重载
- Python 运算符重载
- Python运算符重载
- Python-运算符重载
- Python 运算符重载
- [Python]重载运算符
- python运算符重载
- python运算符重载实例
- 最长子字符串-无重复字符
- js事件
- 快速排序和归并排序的实现
- 工具解析|杀毒引擎惨遭打脸,黑帽大会爆惊天免杀工具
- 事件
- [Python]重载运算符
- 文件系统编程的应用
- murmur2
- 从xml文件中获取相关数据,解决xml中部分数据没有结束的问题
- 《leetCode》:4Sum
- 网络编程
- java中的JSON操作
- 我是如何通过命令执行到最终获取内网Root权限的
- NYOJ 116 士兵杀敌(二)(树状数组模板)