python built-in function study-----repr

来源:互联网 发布:sql sum 不用group by 编辑:程序博客网 时间:2024/06/01 22:48
repr(object)

Return a string containing a printable representation of an object. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.


>>> help(repr)Help on built-in function repr in module builtins:repr(...)    repr(object) -> string        Return the canonical string representation of the object.    For most object types, eval(repr(object)) == object.
example:
>>> class Pair():def __init__(self,x,y):self.x=xself.y=ydef __repr__(self):return('Pair({0.x!r},{0.y!r}'.format(self))def __str__(self):return('{0.x!s},{0.y!s}'.format(self))>>> p=Pair(3,4)>>> pPair(3,4

特殊方法__repr__()返回的是实例的代码表示(code representation)

0 0
原创粉丝点击