码迷,mamicode.com
首页 > 其他好文 > 详细

the difference __str__ and __repr__

时间:2017-11-19 16:04:53      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:int   python   https   over   let   har   diff   first   rate   

 

 

 

 

 

First, let me reiterate the main points in Alex’s post:

  • The default implementation is useless (it’s hard to think of one which wouldn’t be, but yeah)
  • __repr__ goal is to be unambiguous
  • __str__ goal is to be readable
  • Container’s __str__ uses contained objects’ __repr__
>>> class Foo(object):
    def __repr__(self):
        return repr

    
>>> f1=Foo()
>>> f1
repr
>>> print(f1)
repr
>>> str(f1)
repr
>>> repr(f1)
repr
>>> class Foo2(object):
    def __str__(self):
        return repr

    
>>> f2=Foo2()
>>> f2
<__main__.Foo2 object at 0x0000000002FF3F98>
>>> print(f2)
repr
>>> str(f2)
repr
>>> repr(f2)
<__main__.Foo2 object at 0x0000000002FF3F98>

https://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python

the difference __str__ and __repr__

标签:int   python   https   over   let   har   diff   first   rate   

原文地址:http://www.cnblogs.com/yuyang26/p/7859952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!