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__ 的区别的更多相关文章

  1. [Python3 填坑] 002 isdecimal() 与 isdigit() 的区别 + isnumeric() 的补充

    目录 1. print( 坑的信息 ) 2. isdecimal() 官方文档 3. isdigit() 官方文档 4. 举例 (1) 先说结论 (2) 示例 5. 补充 isnumeric() (1 ...

  2. [Python3 填坑] 006 “杠零”,空字符的使用

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 \0 是空字符,输出时看不到它,但它占 1 个字符的长度 2.2 \0 "遇八进制失效" 2.3 \0 与 '' 不 ...

  3. python中__str__与__repr__的区别

    __str__和repr __str__和__repr__都是python的内置方法,都用与将对象的属性转化成人类容易识别的信息,他们有什么区别呢 来看一段代码 from math import hy ...

  4. Django---图书管理系统,一对多(外键设置),__str__和__repr__的区别,进阶版项目说明简介.模版语言if ... else ..endif

    Django---图书管理系统,一对多(外键设置),__str__和__repr__的区别,进阶版项目说明简介.模版语言if ... else ..endif 一丶__str__ 和 __repr__ ...

  5. [Python3 填坑] 014 类的常用魔术方法举例

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 __init__() 2.2 __new__() 2.3 __call__() 2.4 __str__() 2.5 __repr__() ...

  6. [Python3 填坑] 004 关于八进制

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 2.2.1 先说结论 2.2.2 八进制的用途 2.2.3 少废话,上例子 1. print( 坑的信息 ...

  7. [Python3 填坑] 001 格式化符号 & 格式化操作符的辅助指令

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python 格式化符号表 举例说明 (1) %c (2) %s 与 %d (3) %o (4) %x (5) %f (6) %e (7 ...

  8. [Python3 填坑] 012 字典的遍历在 Python2 与 Python3 中区别

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python2 中字典的遍历 2.2 Python3 中字典的遍历 2.3 结论 1. print( 坑的信息 ) 挖坑时间:2019/ ...

  9. [Python3 填坑] 009 深拷贝与浅拷贝

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python3.7 官方文档 2.2 赋值.切片与 copy() 分析 分析 分析 分析 2.3 copy 模块 分析 分析 2.4 小 ...

随机推荐

  1. OPECV的配置

    因为要做一点道路是别的东西,所以想用到OPENCV的一些东西.在网上找了一些OPENCCSHARP的代码,但是这方面的书籍或者资料还是不是特别的多,所以我就觉得可能还不是很好.主要的原因还是因为自己的 ...

  2. 时间戳位数不够13位,通过es6 的padEed补零

    if(val.toString().length == 10){ val = val.toString().padEnd(13,"0")*1 //不够十三位放后面补零,超过13位也 ...

  3. Storm消费Kafka值得注意的坑

    问题描述: kafka是之前早就搭建好的,新建的storm集群要消费kafka的主题,由于kafka中已经记录了很多消息,storm消费时从最开始消费问题解决: 下面是摘自官网的一段话:How Kaf ...

  4. Python基础入门一文通 | Python2 与Python3及VSCode下载和安装、PyCharm激活与安装、Python在线IDE、Python视频教程

    目录 1. 关键词 2. 推荐阅读 2.1. 视频教程 3. 本文按 4. 安装 4.1. 视频教程 4.2. 资源下载 4.3. 安装教程 1. 关键词 Python2 与Python3及VSCod ...

  5. ELK Stack

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11488404.html ELK workflow log -> filebeat -> l ...

  6. NODE升级到V12.X.X

    Node.js 是一个基于Chrome JavaScript运行时的平台,可轻松构建快速,可扩展的网络应用程序.最新版本 node.js yum存储库 由其官方网站维护.使用本教程添加yum存储库,并 ...

  7. shell学习----正则表达式

    在使用sed和gawk时如果能够熟练的使用正则表达式,可以准确的过滤到自己需要的信息 Linux中,有两种流行的正则表达式引擎: POSIX基础正则表达式,BRE引擎 POSIX扩展正则表达式,ERE ...

  8. linux vim设置和 快捷命令配置

    1.vim配置 set tabstop= set shiftwidth= set softtabstop= set fileencodings=utf-,ucs-bom,gb2312,gbk,gb18 ...

  9. Solr JAVA客户端SolrJ的使用

    一.Solrj简介 SolrJ是操作Solr的JAVA客户端,它提供了增加.修改.删除.查询Solr索引的JAVA接口.SolrJ针对 Solr提供了Rest 的HTTP接口进行了封装, SolrJ底 ...

  10. [CSP-S模拟测试]:旋转子段(数学)

    题目描述 $ZYL$有$N$张牌编号分别为$1,2,...,N$.他把这$N$张牌打乱排成一排,然后他要做一次旋转使得旋转后固定点尽可能多.如果第$i$个位置的牌的编号为$i$,我们就称之为固定点.旋 ...