浅谈python中__str__和__repr__的区别
<p>很多时候我们在创建一个类的时候,在终端打印类或者查看的时候一般都不会得到一个太满意的结果</p><div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> T:</span></pre>
<pre> def __init__(self):<br> self.color="red"<br> self.count = 2</pre>
<pre><span style="color: rgba(0, 0, 0, 1)">t </span>=<span style="color: rgba(0, 0, 0, 1)"> T()<br>t <br></span>>>> <T object at 0x0000000003444E10> <br><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(t) <br></span>>>> <T object at 0x0000000003444E10></pre>
</div>
<p> </p>
<p>类转化为字符串,直接打印结果一般都不是我们想要的东西,仅仅包含了类的名称以及实例的 ID (理解为 Python 对象的内存地址即可),例如<T object at 0x0000000003444E10> ,这样类默认转化为字符串根本不能得到我们想要的关于属性信息等的相关东西,只能通过手动打印才能进一步获取到更多的信息,例如print(t.color)</p>
<p><span style="font-size: 16px"><strong>使用 __str__ 实现类到字符串的转化 </strong></span></p>
<p><span style="font-size: 15px">不用自己另外的定义方法,和java的toString的方法类似,可以在类里面实现__str__和__repr__方法,从而自已定义类的字符串描述,下面具体看看这两种方式是怎么工作的。</span></p>
<p><span class="bjh-p">首先,我们先加一个 __str__ 方法到前面的类中看看情况。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> T:
</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__str__</span><span style="color: rgba(0, 0, 0, 1)">(self):
</span><span style="color: rgba(0, 0, 255, 1)">return</span> self.color+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">__str__</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self):
self.color</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">red</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
self.count </span>= 2<span style="color: rgba(0, 0, 0, 1)">
t </span>=<span style="color: rgba(0, 0, 0, 1)"> T()
t
</span>>>> <T object at 0x0000000003444E10>
<span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(t)
</span>>>> red__str__</pre>
</div>
<p>查看 t的时候的输出仍然和之前一样,输出内存地址信息,不过打印 t的时候返回的内容和新加的 __str__ 方法的返回一致。类的 __str__ 方法会在某些 需要将<span style="color: rgba(255, 102, 0, 1)">对象转为字符串</span>的时候被调用。</p>
<p>比如下面这些情况,需要将对象转为字符串</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(t)
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(str(t))
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{}</span><span style="color: rgba(128, 0, 0, 1)">"</span>.format(t))</pre>
</div>
<p> </p>
<p><span style="font-size: 16px"><strong><span class="bjh-p">使用 __repr__ 也有类似的效果 </span></strong></span></p>
<p><span class="bjh-p">会发现,在使用__str__方法的时候,直接在终端查看t对象的时候,输出的时候仍然是内存地址,这是因为在python3中有两种方式控制对象转字符串,第一种就是__str__,第二种就是__repr__,两种的工作方式大体相同,只是调用的时机不同。下面通过一个简单的例子看看__repr__方法</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> T:
</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__repr__</span><span style="color: rgba(0, 0, 0, 1)">(self):
</span><span style="color: rgba(0, 0, 255, 1)">return</span> self.color+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">__repr__</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__str__</span><span style="color: rgba(0, 0, 0, 1)">(self):
</span><span style="color: rgba(0, 0, 255, 1)">return</span> self.color+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">__str__</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self):
self.color</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">red</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
self.count </span>= 2<span style="color: rgba(0, 0, 0, 1)">
t </span>=<span style="color: rgba(0, 0, 0, 1)"> T()
t
</span>>>><span style="color: rgba(0, 0, 0, 1)"> red__repr__
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(t)
</span>>>><span style="color: rgba(0, 0, 0, 1)">red__str__
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(str(t))
</span>>>><span style="color: rgba(0, 0, 0, 1)">red__str__
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">.format(t))
</span>>>><span style="color: rgba(0, 0, 0, 1)">red__str__
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(str([<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">d</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">f</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,t]))
</span>>>> [<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">d</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">f</span><span style="color: rgba(128, 0, 0, 1)">'</span>, red__repr__]</pre>
</div>
<p><span class="bjh-p">通过如上的例子可以看出,<span style="color: rgba(255, 102, 0, 1)">当直接查看对象的时候调用的是__repr__方法,对象需要转字符串的时候调用的是__str__方法,但是当字典列表等容器的时候调用的还是__repr__方法</span></span></p>
<p><span class="bjh-p" style="color: rgba(0, 0, 0, 1)">如果我们需要显示的指定以何种方式进行类到字符串的转化,我们可以使用内置的 str() 或 repr() 方法,它们会调用类中对应的双下划线方法。除了字典、列表等作为容器的时候</span>print(str(["d","f",t])),虽然调用的是str()方法,可是容器是列表,所以对象t转为字符串时调用的就是__repr__</p>
<p><span style="font-size: 16px"><strong>__str__ 和 __repr__ 的差别 </strong></span></p>
<p><span style="font-size: 14px">那么他们的区别具体的在哪里,带着这个问题我们python的标准库, 其中datetime.date 这个类是怎么在使用这两个方法的。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> datetime
today </span>=<span style="color: rgba(0, 0, 0, 1)"> datetime.datetime.today()
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(str(today))
</span>>>> 2019-10-20 20:59:47.003003
<span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(repr(today))
</span>>>> datetime.datetime(2019, 10, 20, 20, 59, 47, 3003)</pre>
</div>
<p><span class="bjh-p"><span style="color: rgba(255, 102, 0, 1)">__str__ 的返回结果可读性强</span>。也就是说,__str__ 的意义是得到便于人们阅读的信息,就像上面的 '</span>2019-10-20 20:59:47.003003<span class="bjh-p">' 一样。</span></p>
<p><span class="bjh-p"><span style="color: rgba(255, 102, 0, 1)">__repr__ 的返回结果应更准确</span>。怎么说,__repr__ 存在的目的在于调试,便于开发者使用。将 __repr__ 返回的方式直接复制到命令行上,是可以直接执行的。</span></p>
<p><span style="font-size: 16px"><strong> 为什么每个类都最好有一个 __repr__ 方法</strong></span></p>
<p><span style="font-size: 14px">如果你想保证每个类到字符串都有一个有效的自定义转换方式,你需要重写__str__或者__repr__中的一个方法,如果没有写__str__方法,python就会去找__repr__方法,所以至少添加一个</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> T:
</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__repr__</span><span style="color: rgba(0, 0, 0, 1)">(self):
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (self.color)
</span><span style="color: rgba(0, 0, 255, 1)">def</span> <span style="color: rgba(128, 0, 128, 1)">__init__</span><span style="color: rgba(0, 0, 0, 1)">(self,color):
self.color </span>=<span style="color: rgba(0, 0, 0, 1)"> color
t </span>= T(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">red</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(t)
</span>>>> red<br>t<br>>>> red<br><span style="color: rgba(0, 0, 255, 1)">print</span>(str(t))<span style="color: rgba(0, 0, 0, 1)"><br>>>> red</span></pre>
</div>
<p><span style="font-size: 14px"> <span class="bjh-p">实现了 __repr__ 方法后,当我们查看类的实例或者直接调用 repr() 方法,就能得到一个比较满意的结果了。打印或直接调用str()等类转字符串的操作都会得到相同的结果,因为<strong><span style="color: rgba(255, 102, 0, 1)">__str__的默认实现就是调用__repr__方法。</span></strong></span></span></p>
<div class="img-container"><span style="color: rgba(0, 0, 0, 1); font-size: 14px">这样就能用比较少的工作量,不用手动的去一一获取信息,就能让两个方法都能功能,而且都具有一定的可读性,所以推荐类中至少添加一个__repr__ 方法。</span></div>
<div class="img-container"><span style="font-size: 14px"><strong><span style="color: rgba(0, 0, 0, 1)">小结</span></strong></span></div>
<ul>
<li class="img-container"><span style="font-size: 14px"><span style="color: rgba(0, 0, 0, 1)">我们可以使用__str__ 和 __repr__中的任意一个方法定义类到字符串的转化方式,不需要手动的打印某些属性和一些额外的信息</span></span></li>
<li class="img-container"><span style="font-size: 14px"><span style="color: rgba(0, 0, 0, 1)">一般来说__str__的可读性更强,而__repr__的返回结果更具有准确性,更加的适合开发者</span></span></li>
<li class="img-container"><span style="font-size: 14px">我们在写类的时候,最好至少添加一个__repr__方法,来保证类到字符串的转换具有自定义的有效性,__str__是可选的,因为在默认情况下,__Str__方法默认实现调用的是__repr__方法,所以在对象转字符串时候,找到底层str方法之后,会调用重写的__repr__方法</span></li>
</ul>
<p><span style="font-size: 14px">也就是说,</span></p>
<p><span style="font-size: 14px"> 直接查看字符串的时候调用的是__repr__方法;</span></p>
<p><span style="font-size: 14px"> 对象在转字符串的时候:如果容器是字典或者列表调用的是__repr__方法;如果是其他情况则默认调用的是底层的__Str__方法。__str__如果被重写调用的则是重写之后__Str__,就和__repr__没有关系了,如果没有被重写,则调用的是底层__str__方法,底层方法默认实现方式是调用了__repr__ 方法,之后查看__repr__是否被重写,重写则调用重写之后的,否则调用底层的__repr__,最后执行转为字符串</span></p>
<p><span style="font-size: 14px"> 如果打印str(t)则默认调用的是__str__,如果直接打印repr(t)则默认调用的是__repr__方法</span></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/xiaoneng/p/11699633.html
頁:
[1]