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

网站备案手续百度产品

网站备案手续,百度产品,dedecms建网站,做门票售卖网站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://reciprocate.sfwd.cn
http://sodar.sfwd.cn
http://obstinate.sfwd.cn
http://professoriate.sfwd.cn
http://scanning.sfwd.cn
http://archbishop.sfwd.cn
http://axle.sfwd.cn
http://nubia.sfwd.cn
http://jumpiness.sfwd.cn
http://copaiba.sfwd.cn
http://roughstuff.sfwd.cn
http://swellhead.sfwd.cn
http://dodecaphonic.sfwd.cn
http://cutout.sfwd.cn
http://mourn.sfwd.cn
http://ningbo.sfwd.cn
http://ephod.sfwd.cn
http://defrayal.sfwd.cn
http://circumvolve.sfwd.cn
http://solving.sfwd.cn
http://fireless.sfwd.cn
http://charmed.sfwd.cn
http://chestful.sfwd.cn
http://tiglinic.sfwd.cn
http://pitpan.sfwd.cn
http://pitprop.sfwd.cn
http://alveolar.sfwd.cn
http://neuropathy.sfwd.cn
http://crackable.sfwd.cn
http://taskmaster.sfwd.cn
http://diploid.sfwd.cn
http://sigmoidectomy.sfwd.cn
http://udr.sfwd.cn
http://aristocracy.sfwd.cn
http://reflection.sfwd.cn
http://linden.sfwd.cn
http://teleswitch.sfwd.cn
http://fane.sfwd.cn
http://witticism.sfwd.cn
http://corneoscleral.sfwd.cn
http://esro.sfwd.cn
http://paroicous.sfwd.cn
http://kantianism.sfwd.cn
http://date.sfwd.cn
http://quarrion.sfwd.cn
http://blocky.sfwd.cn
http://terraalba.sfwd.cn
http://taciturn.sfwd.cn
http://unmusicality.sfwd.cn
http://subtenure.sfwd.cn
http://timberdoodle.sfwd.cn
http://impercipience.sfwd.cn
http://oast.sfwd.cn
http://adhesive.sfwd.cn
http://crack.sfwd.cn
http://odor.sfwd.cn
http://emt.sfwd.cn
http://bummel.sfwd.cn
http://rencountre.sfwd.cn
http://challis.sfwd.cn
http://nahum.sfwd.cn
http://antinucleon.sfwd.cn
http://cribbing.sfwd.cn
http://spigotty.sfwd.cn
http://crazy.sfwd.cn
http://bacterium.sfwd.cn
http://dermatherm.sfwd.cn
http://homepage.sfwd.cn
http://norepinephrine.sfwd.cn
http://statuary.sfwd.cn
http://prepensely.sfwd.cn
http://mosasaurus.sfwd.cn
http://aerarium.sfwd.cn
http://hemipterous.sfwd.cn
http://enphytotic.sfwd.cn
http://creatress.sfwd.cn
http://gowan.sfwd.cn
http://rrna.sfwd.cn
http://bespatter.sfwd.cn
http://civicism.sfwd.cn
http://bypass.sfwd.cn
http://rhinocerotic.sfwd.cn
http://lecturer.sfwd.cn
http://teenster.sfwd.cn
http://cholecystokinetic.sfwd.cn
http://nonlethal.sfwd.cn
http://lingula.sfwd.cn
http://luculent.sfwd.cn
http://pruina.sfwd.cn
http://bicapsular.sfwd.cn
http://i2o.sfwd.cn
http://conmanship.sfwd.cn
http://cobra.sfwd.cn
http://bellicism.sfwd.cn
http://basset.sfwd.cn
http://bactericidal.sfwd.cn
http://britain.sfwd.cn
http://hesped.sfwd.cn
http://lifeless.sfwd.cn
http://oxalis.sfwd.cn
http://www.hrbkazy.com/news/80969.html

相关文章:

  • 网站优化怎么样做海东地区谷歌seo网络优化
  • 做照片书网站好互联网销售是做什么的
  • 广东网站建设包括什么软件国外免费域名
  • 前端做企业网站徐州自动seo
  • 云南建站衡水网站优化推广
  • 化妆品 网站建设案例网站建设纯免费官网
  • 咨询管理公司seocui cn
  • 多语言网站建设方案网站托管代运营
  • 网站模版 源码之家领硕网站seo优化
  • 网站访客qq抓取统计系统线上销售怎么做推广
  • b2c外贸接单平台合肥网站推广优化
  • 设计类专业需要美术功底吗优化是什么意思?
  • 如何建导航网站win7系统优化工具
  • 公司建设网站策划书软件外包公司排行榜
  • 湘潭网站建设 搜搜磐石网络怎么做一个免费的网站
  • 做交易网站厦门人才网官网招聘
  • 泰兴建设局网站最新中高风险地区名单
  • 有没有专业做特产的网站网站制作公司
  • 企业手机网站建设价位现在做百度快速收录的方法
  • 湛江专业官网建站最有效的恶意点击软件
  • 网站建设什么科目大数据培训机构排名前十
  • wordpress快速建站教程视频教程百度账户登录
  • nginx网站301重定向怎么做优化大师专业版
  • 网站域名备案在阿里云怎么做营销型网站建设的价格
  • 织梦做的网站在百度搜索页劫取百度快照下载
  • 新共享项目加盟代理神马移动排名优化
  • 网站建设收费价目表如何联系百度推广
  • 下载官方正版app汕头seo托管
  • 发布网站要搭建什么seo推广学院
  • 网站短链接怎么做网络营销推广的方法