您的位置:首页 > 移动开发 > Objective-C

Python 内建函数 - repr(object)

2017-03-22 16:20 239 查看
Manual

直译

实例

拓展阅读

Manual

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.

直译

返回一个字符串,其包含一个对象的可打印的表述形式。对于多数类型,该函数会尝试返回一个字符串,将其传入eval()后返回的值与原对象值相等;否则,该表述是一个封闭在尖括号内的字符串,其包含对象的类型名称和其他附加信息(通常包括对象的名称和地址)。可以通过定义一个类的__repr__()方法来控制该函数为其实例返回的内容。

实例

>>> a = repr(range(10))
>>> list(eval(a))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> b = repr(pow(2, 3, 5))
>>> eval(b)
3

>>> c = repr((1+2) / 4)
>>> eval(c)
0.75


拓展阅读

eval()

__repr__()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python