Python内置函数(53)——repr
英文文档:
repr(object)- 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.
- 说明:
- 1. 函数功能返回一个对象的字符串表现形式。其功能和str函数比较类似,但是两者也有差异:函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式。
>>> a = 'some text'
>>> str(a)
'some text'
>>> repr(a)
"'some text'"
2. repr函数的结果一般能通过eval()求值的方法获取到原对象。
>>> eval(repr(a))
'some text'
3. 对于一般的类型,对其实例调用repr函数返回的是其所属的类型和被定义的模块,以及内存地址组成的字符串。
>>> class Student:
def __init__(self,name):
self.name = name >>> a = Student('Bob')
>>> repr(a)
'<__main__.Student object at 0x037C4EB0>'
4. 如果要改变类型的repr函数显示信息,需要在类型中定义__repr__函数进行控制。
>>> class Student:
def __init__(self,name):
self.name = name
def __repr__(self):
return ('a student named ' + self.name) >>> b = Student('Kim')
>>> repr(b)
'a student named Kim'
Python内置函数(53)——repr的更多相关文章
- Python内置函数之repr()
repr(object) 返回对象的字符串形式. >>> a = 'hello' >>> repr(a) "'hello'" 返回的字符串形式可 ...
- Python内置函数(53)——setattr
英文文档: setattr(object, name, value) This is the counterpart of getattr(). The arguments are an object ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- Python内置函数(12)——str
英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string ...
- Python内置函数(61)——str
英文文档: class str(object='') class str(object=b'', encoding='utf-8', errors='strict') Return a string ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- python 内置函数总结(大部分)
python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...
随机推荐
- python打包工具 cx_Freeze介绍
原理 Python 脚本在装有 Python 的系统中可以直接双击运行,但绝大多数普通用户并没有配置此类环境,而编译为可执行二进制文件后,用户无需预先安装 Python 及依赖库即可像运行普通程序一样 ...
- symfony小练习-表白墙
过上一个博客系统以及对官方示例程序的基本学习,目前对symfony的各个组件有了一定的学习,学校布置了一个表白墙任务,这里就这个任务的完成进行记录 ...........2019.3.20.22.31 ...
- Laravel修改验证提示信息为中文
1.覆盖提示信息: 打开resource/lang/en/validation.php注释掉英文提示信息 $ sudo vim resource/lang/en/validation.php 将下面的 ...
- 多版本python安装TensorFlow出现的各种事故
TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数 ...
- 首次安装Ubuntu
初试Ubuntu 双系统的安装 situation: dell(2017购) 固态250G+机械硬盘500G 已经安装windows 10 BIOS 为 UEFI rufus(向U盘写入镜像) Ubu ...
- 实验5 Spark SQL 编程初级实践
源文件内容如下(包含 id,name,age),将数据复制保存到 ubuntu 系统/usr/local/spark 下, 命名为 employee.txt,实现从 RDD 转换得到 DataFram ...
- 微软官网tools
DHCP/AD域插件: 远程管理工具(含DHCP/AD域) 安装网址: https://www.microsoft.com/zh-cn/download/details.aspx?id=7887 程序 ...
- avuex
今天做了的avuex终于发现了问题.作为前端小白,解决花了一上午,这是因为以前没有用过框架.还好终于憋出来了.具体如下,还望不要嘲笑自己 查找好久原来是没有仔细看文档的原因,一定要记住,这是一个技术活 ...
- BJOI2019Day1 数据&标程&题解
链接: https://pan.baidu.com/s/16L5GHvo9WtY20sZoqjuQNQ 提取码: 3iur
- BZOJ1991 : Pku2422 The Wolves and the Sheep
将每个不是障碍的格子标号,设三只狼的位置分别为$A,B,C$,羊的位置在$D$.合法状态中强行限制$A<B<C$,这样状态数只有$\frac{n^8}{6}\approx 1.6\time ...