Python 内建函数 - repr(object)

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__()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值