学习Python:repr和str的区分
来源:互联网 发布:谷歌镜像源码 编辑:程序博客网 时间:2024/05/06 21:37
基于Python 2.7.9 Docmentation中repr和str的说明
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.翻译如下:
返回一个表示对象的可打印的字符串。这和通过转换(反引号``)处理得到的结果一致。作为一个普通函数,可以使用这个运算有些时候是有用处的。对于大部分类型,这个函数尝试返回一个字符串,当其传给eval(),将生成同样的对象,(即eval(repr(object)==object.)否则生成一个用尖括号括起来的字符串,包含着对象类型名和通常一些对象名以及对象地址等额外信息。一个类可以通过重新定义__repr__()成员函数来控制自身实例关于这个函数的返回值。
class str(object='')
Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.翻译如下:
返回一个表示对象的可打印的友好的字符串。对于字符串来说,将返回自身。与repr(object)区别在于,str(object)不尝试返回一个传递给eval()的字符串;其目标是返回一个可打印的字符串。如果没有给出参数,返回空字符串。(同理对类,可通过__str__() 成员控制其行为。)
注:eval(str)将字符串str当成有效的表达式来求值并返回计算结果
0 0
- 学习Python:repr和str的区分
- Python的str()和repr()
- python-str()和 repr()
- Python学习笔记-str和repr
- 区别python 的str和repr
- python之repr()和str()的区别
- Python 的repr() 和str() 函数
- python 的repr和str以及“”
- python之repr()和str()的区别
- python中repr和 str的异同
- python str和repr 的区别
- python str和repr的区别
- python str和repr的区别
- python str和repr的区别
- Python str 和 repr 的区别
- python的str()和repr()函数比较
- python中的repr和str
- python中的str()和repr()
- IOS开发博客
- ORACLE DATABASE LINK
- 直接插入排序
- Python基础学习篇—异常处理机制
- sed很强大的文本操作命令
- 学习Python:repr和str的区分
- Android自定义View(一)
- 使用throw抛出异常
- 项目总结之截屏细节考虑
- 什么才是程序员的核心竞争力?
- 使用ALAssetsLibrary读取所有照片
- 配置
- Java模板引擎 FreeMarker
- 几种常用的高效排序(一)--快排