[Python3 填坑] 015 __str__ 与 __repr__ 的区别
1. print( 坑的信息 )
- 挖坑时间:2019/04/07
- 明细
| 坑的编码 | 内容 |
|---|---|
| Py023-3 | __str__ 与 __repr__ 的区别 |
2. 开始填坑
2.1 上例子
>>> class A(object):
... def __str__(self):
... return "this is __str__"
... def __repr__(self):
... return "this is __repr__"
...
>>> a = A()
>>> a
this is __repr__
>>> print(a)
this is __str__
>>>
2.2 关系与区别
- 同时定义
__repr__方法和__str__方法时,print() 方法会调用__str__方法
Python 3.7.3 的官方文档
object.__str__(self)
Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.
This method differs from object.__repr__() in that there is no expectation that __str__() return a valid Python expression: a more convenient or concise representation can be used.
The default implementation defined by the built-in type object calls object.__repr__().
- 最后一句大概是说,
__str__是调用__repr__实现的
object.__repr__(self)
Called by the repr() built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned. The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an “informal” string representation of instances of that class is required.
This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.
- 最后一句大概是说,
__repr__通常用于调试
网上看到一个例子,运行了一下
>>> import datetime
>>> today = datetime.datetime.now()
>>> today
datetime.datetime(2019, 4, 11, 20, 45, 8, 463653)
>>> str(today)
'2019-04-11 20:45:08.463653'
>>> repr(today)
'datetime.datetime(2019, 4, 11, 20, 45, 8, 463653)'
>>>
简单地说
__str__表达更简洁,__repr__显示更全面
[Python3 填坑] 015 __str__ 与 __repr__ 的区别的更多相关文章
- [Python3 填坑] 002 isdecimal() 与 isdigit() 的区别 + isnumeric() 的补充
目录 1. print( 坑的信息 ) 2. isdecimal() 官方文档 3. isdigit() 官方文档 4. 举例 (1) 先说结论 (2) 示例 5. 补充 isnumeric() (1 ...
- [Python3 填坑] 006 “杠零”,空字符的使用
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 \0 是空字符,输出时看不到它,但它占 1 个字符的长度 2.2 \0 "遇八进制失效" 2.3 \0 与 '' 不 ...
- python中__str__与__repr__的区别
__str__和repr __str__和__repr__都是python的内置方法,都用与将对象的属性转化成人类容易识别的信息,他们有什么区别呢 来看一段代码 from math import hy ...
- Django---图书管理系统,一对多(外键设置),__str__和__repr__的区别,进阶版项目说明简介.模版语言if ... else ..endif
Django---图书管理系统,一对多(外键设置),__str__和__repr__的区别,进阶版项目说明简介.模版语言if ... else ..endif 一丶__str__ 和 __repr__ ...
- [Python3 填坑] 014 类的常用魔术方法举例
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 __init__() 2.2 __new__() 2.3 __call__() 2.4 __str__() 2.5 __repr__() ...
- [Python3 填坑] 004 关于八进制
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 2.2.1 先说结论 2.2.2 八进制的用途 2.2.3 少废话,上例子 1. print( 坑的信息 ...
- [Python3 填坑] 001 格式化符号 & 格式化操作符的辅助指令
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python 格式化符号表 举例说明 (1) %c (2) %s 与 %d (3) %o (4) %x (5) %f (6) %e (7 ...
- [Python3 填坑] 012 字典的遍历在 Python2 与 Python3 中区别
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python2 中字典的遍历 2.2 Python3 中字典的遍历 2.3 结论 1. print( 坑的信息 ) 挖坑时间:2019/ ...
- [Python3 填坑] 009 深拷贝与浅拷贝
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python3.7 官方文档 2.2 赋值.切片与 copy() 分析 分析 分析 分析 2.3 copy 模块 分析 分析 2.4 小 ...
随机推荐
- 给当当同学的random data
m**o 00'57"32街**o 00'52"23c**6 00'44"15斗**6 00'57"58n**5 00'32"04s**p 00'51 ...
- python学习笔记(8):
一.变量和类型 1.Python基本变量类型: 整数 ,浮点数 ,字符串, 布尔值 ,空值 ,函数, 模块, 类型, 自定义类型 2.变量定义 :变量存储在内存中的值.这就意味着在创建变量时会在内存中 ...
- 四轴飞行器飞行原理与双闭环PID控制
四轴轴飞行器是微型飞行器的其中一种,相对于固定翼飞行器,它的方向控制灵活.抗干扰能力强.飞行稳定,能够携带一定的负载和有悬停功能,因此能够很好地进行空中拍摄.监视.侦查等功能,在军事和民用上具备广泛的 ...
- 构建LNMP+memcached服务
通过PHP页面实现对memcached服务器的数据操作,实现以下目标: - 为PHP安装memcache扩展 - 创建PHP页面,并编写PHP代码,实现对memcached的数据操作 环境:部署LNM ...
- [CF] 8C Looking for Order
状压模板题 CF难度2000? 我得好好了解一下CF的难度机制了 反正CF的难度比洛谷真实就好了 Code #include<algorithm> #include<iostream ...
- koa2 进阶网站
http://www.ruanyifeng.com/blog/2017/08/koa.html 阮一峰 https://www.itying.com/koa/ koa2中文网 https://blo ...
- selenium 自动化的坑(5)
这次要说的自动化坑是关于<a>标签的,话不多说,先上图: 这个表面上看起来是个输入框,操作的时候需要鼠标先悬停,才会出来下面的选项,刚开始我尝试直接点击,结果当然是失败的了. 注意:我的操 ...
- Ubuntu 14.04 虚拟机配置固定ip地址
Ubuntu 14.04 虚拟机配置固定ip地址: 虚拟机用的NAT方式配置: 1.虚拟机: 虚拟机→设置→网络适配器→NAT模式: 2.虚拟机:编辑→虚拟网络编辑器→更改设置→选择NAT→填入子网I ...
- swagger2接口发布demo
1.目的:使用Swagger2发布接口,ui可操作 2.项目结构 3. 代码 3.1 接口类qinfeng.zheng.api.controller.DemoController package q ...
- JS中的继承(原型链、构造函数、组合式、class类)
1.继承 应注意区分继承和实例化,实例化是生成一个对象,这个对象具有构造函数的属性和方法:继承指的应该是利用父类生成一个新的子类构造函数,通过这个子类构造函数实例化的对象,具有子类的属性和方法,同时也 ...