您的位置:首页 > 编程语言 > Python开发

python 总结之 repr函数

2015-11-03 17:03 232 查看
repr:

repr(object)

Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function. 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.

可以认为它能把一个对象转换成一个可打印的字符串,它特点是对于包含着转义字符的字符串,也能完整打印出来,例如

l = [ 'cd\xc3k\x9b\x1d\x08\x84MXr0!\xd1\xec\x86\xab\x01', 'cw\x1c\x9e\x08\x05`\x8f\x9dfd\xd0\xbfS7\x9dZ\x8f']

直接 print l[i],就会出现乱码,原因是里面的元素含义转义字符

这时候采用repr的函数,它就能完成地把转义字符也打印出来

--------------------------我是测试输出结果的分隔线-----------------------------------------------------------------------

l = [ 'cd\xc3k\x9b\x1d\x08\x84MXr0!\xd1\xec\x86\xab\x01', 'cw\x1c\x9e\x08\x05`\x8f\x9dfd\xd0\xbfS7\x9dZ\x8f']

printl[0]

print repr(l[0])

>>>
cd胟?凪Xr0!鸯啱
'cd\xc3k\x9b\x1d\x08\x84MXr0!\xd1\xec\x86\xab\x01'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: