[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 小 ...
随机推荐
- IE, Chrome和Firefox浏览器 差异对比
最近的项目中使用Extjs5.6, 其中主要的一个特点就是js文件的动态加载,之前使用Firefox浏览器对js文件进行调试,打断点时,只对当次调试有效,刷新之后,由于动态加载的js文件(文件名后面加 ...
- JavaScript基础9——操作DOM树
appendChild()方法:添加子节点到末尾 类似于剪切粘贴的效果 insertBefore(newNode, oldNode)方法:在某个节点之前插入一个节点 newNode为要插入的节点, ...
- Anaconda 安装及Python 多版本间切换
安装 Anaconda 安装anaconda 安装较为简单,这里参考官方文档:https://docs.continuum.io/anaconda/install/linux.html 在文件目录下执 ...
- LinuxC语言实现ATM取款机实验Socket
链接:https://pan.baidu.com/s/1sZt4qhYc6CDJVpoJHbtw-Q 提取码:53ot 复制这段内容后打开百度网盘手机App,操作更方便哦 本实验用的是Centos7t ...
- shell脚本--expect自动应答
expect自动应答 TCL语言 需求1:远程登录到A主机,什么事情也不做 #! /usr/bin/env expect # 开启一个程序 spawn ssh root@192.144.213.11 ...
- [python 学习] logging模块
1.将简单日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.war ...
- CSS3 结构性伪类选择器(2)
CSS3 结构性伪类选择器—first-child “:first-child”选择器表示的是选择父元素的第一个子元素的元素E.简单点理解就是选择元素中的第一个子元素,记住是子元素,而不是后代元素. ...
- MySQL修改密码方法汇总
本文给大家汇总介绍了MySQL修改密码的方法,分为MySQL5.7版本之前以及MySQL5.7版本之后的修改方法,有需要的小伙伴可以参考下 MySQL5.7版本之前修改密码的方法: 方法1: 用SET ...
- [CF959D]Mahmoud and Ehab and another array construction task题解
解法 非常暴力的模拟. 一开始吧\(1 -> 2 \times 10^6\)全部扔进一个set里,如果之前取得数都是与原数组相同的,那么lower_bound一下找到set中大于等于它的数,否则 ...
- WINDOWS2008server安全策略设置
一.防止黑客或恶意程序暴力破解我的系统密码 答: 暴力破解Windows密码实质上是通过穷举算法来实现,尤其是密码过于简单的系统,暴力破解的方法还是比较实用的.有一点需要我们注意,这个问题的关键在于W ...