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'
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'
相关文章推荐
- 王亟亟的Python学习之路(五)-dictionary,set,函数,函数参数
- 王亟亟的Python学习之路(五)-dictionary,set,函数,函数参数
- Python Imaging Library
- Python正则表达式指南
- windows下python开发环境搭建
- 【python】python中__name__的使用
- python-操作mssql数据库
- 初用python爬取麦x学院python教学视频
- Anaconda对内置包进行更新
- python中的enumerate函数
- Ansible
- python 环境配置
- Python中使用MySQL
- Windows下Python自动补全
- python第二章-2.7 更多关于方法定义
- 2015/11/3用Python写游戏,pygame入门(3):字体模块、事件显示和错误处理
- 源码安装pysvn
- Python/scikit-learn机器学习库(SVM支持向量机)
- python随机读取文件中的某一行内容
- Sublime2 for Mac OSX配置(Python developer)