当前位置: 首页 > news >正文

沈阳沈阳建设工程信息网站网站ftp根目录

沈阳沈阳建设工程信息网站,网站ftp根目录,镇江网站建设 的公司,浙江信息港官网1 python运算符重载之字符串显示和右侧加法 1.1 重载字符串显示 1.1.1 str和repr python调用prin()t时,自动调用__str__和__repr__, python调用str()时,自动调用__str__和__repr__, python调用repr()时,自动调用_…

1 python运算符重载之字符串显示和右侧加法

1.1 重载字符串显示

1.1.1 str和repr

python调用prin()t时,自动调用__str__和__repr__,

python调用str()时,自动调用__str__和__repr__,

python调用repr()时,自动调用__repr__,不调用 str

终端用__str__,开发时用__repr__。

自定义__str__和__repr__时,必须返回字符串。

>>> class MyAdd:def __init__(self,value=0):self.data=valuedef __add__(self,other):self.data+=other
>>> class MyAddRepr(MyAdd):def __repr__(self):return 'MyAddRepr({})'.format(self.data)
>>> ma=MyAdd(1)
>>> print(ma)
<__main__.MyAdd object at 0x03869C90> 
>>> mar=MyAddRepr(1)
# print 自动调用 __repr__
>>> print(mar)
MyAddRepr(1)
# str repr 自动调用 __repr__
>>> str(mar),repr(mar)
('MyAddRepr(1)', 'MyAddRepr(1)')>>> class MyAddStr(MyAdd):def __str__(self):return 'MyAddStr({})'.format(self.data)
>>> mas=MyAddStr(2)
# print 自动调用 __str__
>>> print(mas)
MyAddStr(2)
# str 自动调用 __str__
# repr 不会调用 __str__
>>> str(mas),repr(mas)
('MyAddStr(2)', '<__main__.MyAddStr object at 0x03869CD0>')>>> class MyAddBoth(MyAdd):def __str__(self):return 'MyAddBothstr({})'.format(self.data)def __repr__(self):return 'MyAddBothrepr({})'.format(self.data)>>> mab=MyAddBoth(3)
# print str 自动调用 __str__
# repr 自动调用 __repr__
>>> print(mab)
MyAddBothstr(3)
>>> str(mab),repr(mab)
('MyAddBothstr(3)', 'MyAddBothrepr(3)')

1.1.2 自定义repr

当print对象在顶层时会调用自定义__str__,非顶层时调用默认调用内容或调用__repr__。

所以,建议自定义__repr__,来统一拦截print(),str(),repr()操作。

>>> class MyPrintStr:def __init__(self,val):self.val=valdef __str__(self):return str(self.val)
>>> class MyPrintRepr:def __init__(self,val):self.val=valdef __repr__(self):return str(self.val)
# 实例非顶层时print时用默认打印,或者repr,而不会用str
>>> mpsl=[MyPrintStr(1),MyPrintStr(2)]
>>> mprl=[MyPrintRepr(1),MyPrintRepr(2)]
>>> print(mpsl)
[<__main__.MyPrintStr object at 0x009F8370>, <__main__.MyPrintStr object at 0x009F8430>]
>>> print(mprl)
[1, 2]

1.2 重载右侧和原处加法

1.2.1 radd

实例在加号左侧,自动调用 add

实例在加号右侧,自动调用 radd

>>> class MyRadd:def __init__(self,val):self.val = valdef __add__(self,other):print('add',self.val,other)return self.val+otherdef __radd__(self,other):print('radd',self.val,other)return other+self.val
>>> x=MyRadd(8)
>>> y=MyRadd(9)
# 实例 在加号左边, 自动调用 __add__
>>> x+1
add 8 1
9
# 实例 在加号右边, 自动调用 __radd__
>>> 1+y
radd 9 1
10
# 两个实例相加时, 先调用 add 再调用 radd
>>> x+y
add 8 <__main__.MyRadd object at 0x00A23A30>
radd 9 8
17# return 类实例时,需要类型测试isinstance,避免嵌套循环
>>> class MyRaddIf:def __init__(self,val):self.val = valdef __add__(self,other):print('add',self.val,other)if isinstance(other,MyRaddIf):other=other.valreturn MyRaddIf(self.val+other)def __radd__(self,other):print('radd',self.val,other)return MyRaddIf(other+self.val)def __repr__(self):return '<MyRaddIf:{}>'.format(self.val)>>> x=MyRaddIf(8)
>>> y=MyRaddIf(9)
>>> print(x+10)
add 8 10
<MyRaddIf:18>
>>> print(10+y)
radd 9 10
<MyRaddIf:19>
>>> z=x+y
add 8 <MyRaddIf:9>
>>> print(z)
<MyRaddIf:17>
#>>> print(z)#注释 if isinstance(other,MyRaddIf) 时,发生嵌套
#<MyRaddIf:<MyRaddIf:17>>
>>> print(z+10)
add 17 10
<MyRaddIf:27>
>>> print(z+z)
add 17 <MyRaddIf:17>
<MyRaddIf:34>
>>> class MyRadd:def __init__(self,val):self.val = valdef __add__(self,other):print('add',self.val,other)return self.val+other
>>> x=MyRadd(8)
>>> x+1
add 8 1
9
# 没有radd时,实例在右侧会报错
>>> 1+x
Traceback (most recent call last):File "<pyshell#127>", line 1, in <module>1+x
TypeError: unsupported operand type(s) for +: 'int' and 'MyRadd'

1.2.2 iadd

python的+=优先调用__iadd___,没有再调用_add__。

>>> class MyIadd:def __init__(self,val):self.val=valdef __iadd__(self,other):self.val+=otherreturn self
# += 调用 __add__
>>> x=MyIadd(5)
>>> x+=1
>>> x.val
6
>>> class MyIadd:def __init__(self,val):self.val=valdef __add__(self,other):self.val+=otherreturn self
# += 调用 __add__
>>> x=MyIadd(5)
>>> x+=1
>>> x.val
6
>>> class MyIadd:def __init__(self,val):self.val=valdef __add__(self,other):print('__add__')self.val+=otherreturn selfdef __iadd__(self,other):print('__iadd__')self.val+=otherreturn self
# += 优先调用 __iadd__
>>> x=MyIadd(5)
>>> x+=1
__iadd__
http://www.yayakq.cn/news/952850/

相关文章:

  • 茂名建设公司网站网站后台管理怎么做
  • 西安网站开发公司浙江新手网络推广
  • 网站源码安装安装wordpress的目录改变了
  • wordpress老站开启多站点asp.net mvc 企业网站
  • 网站的建设费用预算哪个网站做新中式
  • 快速搭建网站的工具有哪些哈尔滨市工程建设招投标网
  • 汽车制造行业网站模板镇江网页设计培训
  • 网站logo做黑页影楼网站建设
  • 作网站流程wordpress mu 安装
  • 佛山营销网站建设联系方式天美大象果冻星空的制作方法
  • 关于网站建设需要了解什么东西济南新风向网站建设
  • 网站软件开发百度推广二级代理商
  • 网站设计如何做动漫设计专业属于什么大类
  • 唐山哪个公司可以制作网站个人开发小程序多少钱
  • 网站域名服务器查询怎么做网站首页关键词
  • asp 网站运行wordpress footer设置
  • 泾川县住房和城乡建设局网站英语作文网站
  • 国外做多媒体展览的网站松江网站设计
  • 站牛网建设一个网站首先需要什么
  • 集团网站品牌建设特点广州网站建设商家
  • 水印网站和县网站定制
  • 那些外贸网站个人可以做比较好的平面设计网站
  • 做豆腐交流经验的网站晋城做网站公司
  • 南昌专门做网站的人淘宝客网站程序购米
  • 北京网站建设著名公司手机如何做api网站
  • 制作网站的花多少钱星空无限传媒在线观看电视剧赘婿
  • 辽宁建设工程信息网官网新网站如何进入wordpress文章内容宽度
  • 网站重新备案怎么做网站需求分析网站建设
  • 温州微网站制作公司电话wordpress做ftp
  • 百度搜索网站图片法国化妆品进口报关做网站