Python运算符重载用法实例分析
本文实例讲述了Python运算符重载用法。分享给大家供大家参考。具体如下:
在Python语言中提供了类似于C++的运算符重在功能:
一下为Python运算符重在调用的方法如下:
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],
__len__ 长度 len(X)
__cmp__ 比较 X==Y,X<Y
__lt__ 小于 X<Y
__eq__ 等于 X=Y
__radd__ Right-Side + +X
__iadd__ += X+=Y
__iter__ 迭代 For In
1. 减法重载
class Number:
def __init__(self, start):
self.data = start
def __sub__(self, other): #minus method
return Number(self.data - other)
number = Number(20)
y = number – 10 # invoke __sub__ method
class Number:
def __init__(self, start):
self.data = start
def __sub__(self, other): #minus method
return Number(self.data - other)
number = Number(20)
y = number – 10 # invoke __sub__ method
2. 迭代重载
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 __getitem__(self, index): #iter override
return index ** 2
X = indexer()
X[2]
for i in range(5):
print X[i]
3. 索引重载
class stepper:
def __getitem__(self, i):
return self.data[i]
X = stepper()
X.data = 'Spam'
X[1] #call __getitem__
for item in X: #call __getitem__
print item
class stepper:
def __getitem__(self, i):
return self.data[i]
X = stepper()
X.data = 'Spam'
X[1] #call __getitem__
for item in X: #call __getitem__
print item
4. 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
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
5. 打印重载
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(%s)' % self.data
x = addrepr(2) #run __init__
x + 1 #run __add__
print x #run __repr__
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(%s)' % self.data
x = addrepr(2) #run __init__
x + 1 #run __add__
print x #run __repr__
6. Call调用函数重载
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)
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)
7. 析构函数重载
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__
希望本文所述对大家的Python程序设计有所帮助。
数据分析咨询请扫描二维码
CDA数据分析师在中国航信高科技产业园进行了面向测试度量的数据分析培训课程,培训人数近2 ...
2024-05-01CDA数据分析师走进深圳迈瑞生物医疗电子股份有限公司,在迈瑞总部展开了为期两天的培训,本次课程参训人员线上及线下近百人, ...
2024-05-01CDA数据分析师在合肥市对合肥阳光新能源科技有限公司开展了为期8天的企业内训。 合肥阳光新能源科技 ...
2024-05-01CDA数据分析师走进海尔大学,进行了《数据治理与数据中台建设的道与术》专题培训,培训现场爆满,近百人参加了此次培训。 ...
2024-05-01在中国银行苏州分行培训中心开始数据分析师培训,此次培训课程共10天内容,包括Excel、MySQL、概率论与数理统计、SPSS等内容, ...
2024-05-01从实际的业务需求出发,结合行业的典型应用特点,围绕实际的商业问题,探讨数据挖掘、机器学习模型在金融领域的应用,包括获客、信用评分、细分画像、交叉销售、反欺诈、违规识别、时序预测、运筹优化、流程挖掘九个方面,形成 ...
2024-05-01本次培训课程为线上+线下的模式,由于学员编程能力不一、部分学员没有编程基础,故提供统计学、python基 ...
2024-05-01华夏银行信用卡中心-机器学习培训 1、课程亮点 取材于业界一流企业和顶级咨询公司的行业实践;已经被证明是人人 ...
2024-05-01主 题:数据中台建设及数据分析应用主题分享 1. 数据中台市场洞察 2. 主流数据中台产品比较 3. 某企业数据中 ...
2024-05-01围绕“数据驱动”战略,全力打造我行 300 人数字化人才梯队,着力培养数字化管理人才、大数据专业团队 ...
2024-05-01在当今数据驱动的商业环境中,数据分析成为了企业决策的重要依据。通过对大量数据的收集、处理和分析,企业能够更好地理解市场 ...
2024-04-29在人工智能(AI)的世界里,提示词(Prompt)是一种强大的工具,它能够引导AI按照用户的需求产生特定的输出。本文将深入探讨AI ...
2024-04-29CDA立足未来职场,拓展前沿视野——对外经贸大学保险学院举办“三全育人大讲堂”分享行业最新动态。 ...
2024-04-294月2日,CDA数据分析师创始发起人兼协会理事长赵坚毅博士受邀在浙江万里学院举办了一场以“数字化能力在职场中的作用” ...
2024-04-29随机森林(Random Forests)现在机器学习中比较火的一个算法,是一种基于Bagging的集成学习方法,能够很好地处理分类和回归的问 ...
2022-12-23方差分析是数据分析中常用的一种统计分析方法,接下来让我们简单了解一下方差分析的基本思想和原理吧。 方差分析(Analysis ...
2022-12-23来源:关于数据分析与可视化 关于streamlit-aggrid 数据排序 表格样式的调整 数据 ...
2022-08-03作者:麦叔 定义 「把上面晦涩的概念汇成一句话就是:」 ❝ 回调函数就是一个被作为参 ...
2022-08-03现今,高学历人群日益增多,物以稀为贵的高学历光环淡去。无论本科生还是研究生,甚至博士生,求职竞争力都大不如前,就业压力越来越大。
2022-06-01某家企业10个人面试,有9个本科生……如何脱颖而出,除得体的举止和良好的沟通力外,证书成重要筹码,这也是很多人考证的关键所在。
2022-04-14