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 ...
随机推荐
- [3]第二章 C++编程简介
(本资料均从 internet 上进行收录整理,若要转载,请与原作者联系) 2.1 机器语言.汇编语言和高级语言 程序员用各种编程语言编写指令,有些是计算机直接理解的,有些则需要中间翻译(tranl ...
- F7里利用DIV 模拟 textarea 显示回行的问题解决
<div class="card-content-inner" style="word-wrap:break-word;word-break:break-all;w ...
- js实现页面重新加载
https://blog.csdn.net/wangjian530/article/details/80596801
- 打包github上的项目,并在本地使用
在GitHub上去找工具并把地址克隆下来 在本地创建一个文件夹,文件夹内右击 (建议路径为英文,并保证本机有github的软件: https://jingyan.baidu.com/article/9 ...
- 七、OpenStack—dashboard组件安装
1.安装包# yum install openstack-dashboard2.编辑 /etc/openstack-dashboard/local_settings 文件 需要更改的几处内容如下: ) ...
- 从Facebook数据泄露事件看大数据时代的个人信息安全问题
进入21世纪后,互联网开始大规模普及,线上业务和线上服务也开始逐渐走入人们的生活.尤其在智能手机和移动互联网诞生以后,人们对网络的依赖更是与日俱增.然而,伴随而来的则是涉及个人隐私的信息安全问题.个人 ...
- MonggoDB(二)
分组聚合 如果你有数据存储在MongoDB中,你想做的可能就不仅仅是将数据提取出来这么简单,可能需要对数据进行分析并加以利用. 聚合框架:可以使用多个构件创建一个管道,上一个构件的结果传给下一个构件. ...
- 如何快速求解第一类斯特林数--nlog^2n + nlogn
目录 参考资料 前言 暴力 nlog^2n的做法 nlogn的做法 代码 参考资料 百度百科 斯特林数 学习笔记-by zhouzhendong 前言 首先是因为这道题,才去研究了这个玩意:[2019 ...
- DW1000 用户手册中文版 第5章 媒体访问控制(帧过滤)
由于已经在wode中排版无法直接复制到博客中,故本节博客发布使用了图片. PDF下载 http://bphero.com.cn/forum.php?mod=viewthread&tid=68
- 下载Spring4.3.18.RELEASE的官方文档
wget -p --page-requisites --convert-links -P /root/spring https://docs.spring.io/spring/docs/4.3.18. ...