您的位置:首页 > 编程语言 > Python开发

【再回首Python之美】【类-特殊方法】__str__()和__repr__()的区别和重写覆盖

2018-02-23 16:29 561 查看
注:使用方法直接跳看:使用总结
__str__(self)和__repr__(self)是类的特殊方法,和类的__init__(self)类似,都是可以重写覆盖的。
6种使用方法 >>>f

>>>f.__repr__()

>>>f.__str__()

>>>print f

>>>print f.__repr__()

>>>print f.__str__()区别和结果总结:
    1)一个普通的类

        上面几种方法的结果输出一样:都是类对象的地址信息。
        详见下面RoundFloat_com类

    2)只重写覆盖了__repr__()的类

        上面几种方法的结果输出一样:都是调用重写的__repr__()
        详见下面RoundFloat_override_repr类

    3)只重写覆盖了__str__()的类
         上面几种方法的结果输出有两类:一个是类对象地址信息和调用重写的__str__() 
        >>> f,>>> f.__repr__(),>>> print f.__repr__()这三个输出类对象的地址信息
        >>>f.__str__(),>>>print f,>>>print f.__str__()这三个调用重写的__str__()

         详见下面RoundFloat_override_str类
     4)同时重写了__repr__()和__str__()的类
         上面几种方法的结果输有两类:一个是调用重写的__repr__(),一个是调用重写的__str__()
         >>> f,>>> f.__repr__(),>>> print f.__repr__()调用重写的__repr__()
         >>> f.__str__(),>>> print f,>>> print f.__str__()调用重写的__str__()
         详见下面RoundFloat_override_both类
结果:
       >>>f                         优先调用顺序:重写的__repr__()--->默认的__repr__()。
       >>>f.__repr__()         优先调用顺序:重写的__repr__()--->默认的__repr__()。
       >>>print f.__repr__() 优先调用顺序:重写的__repr__()--->默认的__repr__()。

       >>>print f                优先调用顺序:重写的__str__()--->重写的__repr___()--->默认的__str__()。
       >>>f.__str__()            优先调用顺序:重写的__str__()--->重写的__repr__()--->默认的__str__()。

       >>>print f.__str__()    优先调用顺序:重写的__str__()--->重写的__repr__()--->默认的__str__()。

根据以上关系可知,得到以下关系:
        >>>f
        >>>f.__repr__()
        >>>print f.__repr__()            这三个只和__repr__()相关

        >>>print f、
        >>>f.__str__()、
        >>>print f.__str__()               这三个和__str__()、__repr__()相关,调用时优先考虑重写的__str__()
使用总结:
    只重写__str__()并设置__repr__ = __str__,就可以保证上面使用方法的结果都是调用重写的__str__()。>>>
>>> class RoundFloat_override_str_reset_repr(object):
def __init__(self, value):
try:
assert isinstance(value, float), "Value must be a float."
self.value = round(value,2)
except:
print "param error!"
def __str__(self):
return 'invoke __str__():%f' % self.value

__repr__ = __str__

>>> f = RoundFloat_override_str_reset_repr(8.8888)
>>> f
invoke __str__():8.890000
>>> f.__repr__()
'invoke __str__():8.890000'
>>> f.__str__()
'invoke __str__():8.890000'
>>> print f
invoke __str__():8.890000
>>> print f.__repr__()
invoke __str__():8.890000
>>> print f.__str__()
invoke __str__():8.890000
>>>
Python Shell端测试结果>>>
>>> class RoundFloat_com(object):
def __init__(self, value):
try:
assert isinstance(value, float), "Value must be a float."
self.value = round(value, 2)
except:
print "param error!"

>>> f = RoundFloat_com(1.666)
>>> f
<__main__.RoundFloat_com object at 0x0000000002D60400>
>>> f.__repr__()
'<__main__.RoundFloat_com object at 0x0000000002D60400>'
>>> f.__str__()
'<__main__.RoundFloat_com object at 0x0000000002D60400>'
>>> print f
<__main__.RoundFloat_com object at 0x0000000002D60400>
>>> print f.__repr__()
<__main__.RoundFloat_com object at 0x0000000002D60400>
>>> print f.__str__()
<__main__.RoundFloat_com object at 0x0000000002D60400>
>>>
>>>
>>>
>>>
>>>
>>> class RoundFloat_override_repr(object):
def __init__(self, value):
try:
assert isinstance(value, float), "Value must be a float."
self.value = round(value, 2)
except:
print "param error!"
def __repr__(self):
return 'invoke __repr__() %f' % self.value

>>> f = RoundFloat_override_repr(2.666)
>>> f
invoke __repr__() 2.670000
>>> print f
invoke __repr__() 2.670000
>>> f.__repr__()
'invoke __repr__() 2.670000'
>>> f.__str__()
'invoke __repr__() 2.670000'
>>> print f
invoke __repr__() 2.670000
>>> print f.__repr__()
invoke __repr__() 2.670000
>>> print f.__str__()
invoke __repr__() 2.670000
>>>
>>>
>>>
>>>
>>>
>>> class RoundFloat_override_str(object):
def __init__(self, value):
try:
assert isinstance(value, float), "Value must be a float."
self.value = round(value, 2)
except:
print "param error!"
def __str__(self):
return 'invoke __str__():%f' % self.value

>>> f = RoundFloat_override_str(3.666)
>>> f
<__main__.RoundFloat_override_str object at 0x0000000002D60518>
>>> print f
invoke __str__():3.670000
>>> f.__repr__()
'<__main__.RoundFloat_override_str object at 0x0000000002D60518>'
>>> f.__str__()
'invoke __str__():3.670000'
>>> print f
invoke __str__():3.670000
>>> print f.__repr__()
<__main__.RoundFloat_override_str object at 0x0000000002D60518>
>>> print f.__str__()
invoke __str__():3.670000
>>>
>>>
>>>
>>>
>>>
>>> class RoundFloat_override_both(object):
def __init__(self, value):
try:
assert isinstance(value, float), "Value must be a float."
self.value = round(value,2)
except:
print "param error!"
def __repr__(self):
return 'invoke __repr__():%f' % self.value
def __str__(self):
return 'invoke __str__():%f' % self.value

>>> f = RoundFloat_override_both(4.666)
>>> f
invoke __repr__():4.670000
>>> print f
invoke __str__():4.670000
>>> f.__repr__()
'invoke __repr__():4.670000'
>>> f.__str__()
'invoke __str__():4.670000'
>>> print f
invoke __str__():4.670000
>>> print f.__repr__()
invoke __repr__():4.670000
>>> print f.__str__()
invoke __str__():4.670000
>>>
>>>
>>>
>>>
>>> (end)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: