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. 2019-8-14-win10-使用-SMB-v1

    title author date CreateTime categories win10 使用 SMB v1 lindexi 2019-08-14 08:55:55 +0800 2018-2-13 ...

  2. pg_ctl - 启动,停止和重启 PostgreSQL 服务器

    SYNOPSIS pg_ctl start [ -w ] [ -s ] [ -D datadir] [ -l filename] [ -o options] [ -p path] pg_ctl sto ...

  3. manjaro软件源报错 不停看到错误 "PackageName: signature from "User <email@archlinux.org>" is invalid" 的几种解决方法

    对于报错情况, 格式大致如下: error: PackageName: signature from "User <email@archlinux.org>" is i ...

  4. [转]Tomcat9.0安装教程 Tomcat9.0环境变量配置教程

    [转]Tomcat9.0安装教程 Tomcat9.0环境变量配置教程 [转]超详细MySQL安装及基本使用教程

  5. android 打卡 虚拟定位 sqlite

    1.使用android5.1模拟器 android5.1模拟器使用数据库管理参数文件,6.0及以后的版本使用xml文件管理 2.使用sqlite修改配置文件 3.修改secure库中的android_ ...

  6. 【串线篇】spring boot日志使用

    一.默认配置 1.SpringBoot默认帮我们配置好了日志: //记录器 Logger logger = LoggerFactory.getLogger(getClass()); @Test pub ...

  7. css图像拼合技术(精灵图)

    CSS图像拼合技术 1.图像拼合 图像拼合技术就是单个图像的集合. 有很多图片的网页可能会需要很多时间来加载和生成多个服务器的请求. 使用图像拼合会降低服务器的请求数量,并节省带宽. 图像拼合实例 有 ...

  8. GPS通讯 数据包解析

    全球时区的划分: 每个时区跨15°经度.以0°经线为界向东向西各划出7.5°经度,作为0时区.即0时区的经度范围是7.5°W——7.5°E.从7.5°E与7.5°W分别向东.向西每15°经度划分为一个 ...

  9. yolo v3 loss=nan, Avg loss=nan的一种原因

    我这里是由于数据整理错误导致的,同一标注区域重复2次送入模型,具体如下: 0.798046875 0.5555555555555556 0.04296875 0.03611111111111111 0 ...

  10. Python3解leetcode N-ary Tree Level Order Traversal

    问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...