repr(object)
返回对象的字符串形式。
>>> a = 'hello'
>>> repr(a)
"'hello'"
返回的字符串形式可以通过 eval()函数获取到本来的值。
>>> eval(repr(a))
'hello'
对于一般的实例对象,返回的是由模块、类型及内存地址组成的字符串对象。
可以通过定义类的私有方法 __repr__()来自定义返回的值。
>>> class A:
... def __init__(self,name):
... self.name = name
... def __repr__(self):
... return ('hello' + self.name)
...
>>> a = A('tom')
>>> repr(a)
'hellotom'