哪个网站做简历比较好,字体排版网站,深圳住房和建设局网站 龙华,肇庆做网站gdmkd1、打印操作会首先尝试__str__和str内置函数#xff0c;他通常返回一个用户友好显示。__repr__用于所有其他环境#xff0c;用于交互式模式下提示回应以及repr函数#xff0c;如果没有使用__str__#xff0c;则会使用print和str。它通常返回一个编码字符串#xff0c;可以…1、打印操作会首先尝试__str__和str内置函数他通常返回一个用户友好显示。__repr__用于所有其他环境用于交互式模式下提示回应以及repr函数如果没有使用__str__则会使用print和str。它通常返回一个编码字符串可以用来重新创建对象或则给开发者一个详细的显示。总而言之除了当定义一个__str__的时候使用print和str。然而需要注意如果没有定义__str__打印还是使用__repr__反过来不成立。在交互模式下只适用__repr__,并且根本不尝试__str__.2、__str__,__repr__都必须返回字符串返回其他类型会抱错。还有一个需要注意的是__str__的用户友好显示可能只有对象出现在一个打印操作的顶层时候才应用嵌套 到较大对象中 的对象 可能用其__repr__或默认方法打印。例如class Printer:def __init__(self,val):self.val valdef __str__(self):return str(self.val)obj [Printer(2),Printer(3)]for x in obj:print(x) #输出2 3print(obj) #输出[__main__.printer object at, __main__.printer object at]但是__repr__能够解决前面提的那个问题class Printer:def __init__(self,val):self.val valdef __repr__(self):return str(self.val)obj [Printer(2),Printer(3)]for x in obj:print(x) #输出2 3print(obj) #输出233、右侧加法和原处加法:__radd__,__iadd__class adder:def __init__(self,value0):self.data valuedef __add__(self,,other):self.dataotheradder只是在左边是对象时使用如果要支持右侧使用实例对象则需要重载 __radd__.例如:class Commuter:def __init__(self,val):self.val valdef __add__(self,other):print(add,self.val,other)return self.valotherdef __radd__(self,other):print(radd,self.val,other)return otherself.val #这个 顺序无所谓只是在调用的时候很位置很重要x Commuter(88)y Commuter(99)print(x1)print(#*8)print(1y)print($*8)print(xy)#输出结果如下add 88 189########radd 99 1100$$$$$$$$add 88 __main__.commuter object atradd 99 88187原处加法: __iadd__或者__add__但是__iadd__更高效class Number:def __init__(self,val):self.val valdef __iadd__(self,other):self.valotherreturn selfx Number(5)x1x1print(x.val) #输出7或者class Number:def __init__(self,val):self.val valdef __add__(self,other):return Number(self.valother)4、call表达式__call__有关python的__call__在官方文档上有这么一句解释 (http://docs.python.org/reference/datamodel.html?highlight__call__#object.__call__)object.__call__(self[, args...])Called when the instance is “called” as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x.__call__(arg1, arg2, ...).当把一个实例当作方法来调用的时候形如instance(arg1,args2,...)那么实际上调用的就是 instance.__call__(arg1,arg2,...)例如class Prod:def __init__(self,value):self.value valuedef __call__(self,other):return self.value *otherx Prod(2)print(x(3)) #输出65、比较__lt__,__gt__和其他方法比较运算没有位置限制。比较运算没有隐士关系比如说 为真并不以为着 !为假python2.6中如果没有定义更为具体的比较方法对其使用一个__cmp__或者内置cmp函数。cmp( x, y)Compare the two objects x and y and return an integer according to the outcome. The return value is negative if x y, zero if x y and strictly positive if x y.class C:def __init__(self,data):self.data datadef __gt__(self,other):return self.data otherdef __lt__(self,other):return self.data otherx C(spam)print(1,x ham)print(2,ham x)print(3,x ham)print(4,ham y C(ham)print(5,xprint(6,xy)6、布尔测试:__bool__,__len__在布尔环境中python首先尝试__bool__来获取一个直接布尔值然后如果没有该方法就尝试__len__类根据对象的长度确定 一个真值。7、对象析构函数__del__每当实例产生时就会调用__init__构造函数每当 实例空间被回收时就会执行析构函数。class Life:def __init__(self,nameunknow):print(hello ,name)self.name namedef __del__(self):print(Goodbye,self.name)brain Life(Brain)#输出 hello brainbrain loretta #输出 Goodbye brain