First, let me reiterate the main points in Alex’s post:

  • The default implementation is useless (it’s hard to think of one which wouldn’t be, but yeah)
  • __repr__ goal is to be unambiguous
  • __str__ goal is to be readable
  • Container’s __str__ uses contained objects’ __repr__
>>> class Foo(object):
def __repr__(self):
return 'repr' >>> f1=Foo()
>>> f1
repr
>>> print(f1)
repr
>>> str(f1)
'repr'
>>> repr(f1)
'repr'
>>> class Foo2(object):
def __str__(self):
return 'repr' >>> f2=Foo2()
>>> f2
<__main__.Foo2 object at 0x0000000002FF3F98>
>>> print(f2)
repr
>>> str(f2)
'repr'
>>> repr(f2)
'<__main__.Foo2 object at 0x0000000002FF3F98>'

https://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python

the difference __str__ and __repr__的更多相关文章

  1. What is the difference between __str__ and __repr__ in Python

    from https://www.pythoncentral.io/what-is-the-difference-between-__str__-and-__repr__-in-python/ 目的 ...

  2. python中__str__与__repr__的区别

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

  3. python 的特殊方法 __str__和__repr__

    __str__和__repr__ 如果要把一个类的实例变成 str,就需要实现特殊方法__str__(): class Person(object): def __init__(self, name, ...

  4. __str__与__repr__

    在讲解之前,我们先来了解下str和repr的区别:两者都是用来将数字,列表等类型的数据转化为字符串的形式.不同之处在于str更加类似于C语言中使用printf输出的内容,而repr输出的内容会直接将变 ...

  5. python之反射和内置函数__str__、__repr__

    一.反射 反射类中的变量 反射对象中的变量 反射模块中的变量 反射本文件中的变量 .定义:使用字符串数据类型的变量名 来获取这个变量的值 例如: name = 'xiaoming' print(nam ...

  6. python __str__() 和 __repr__()是干啥的

    1. 没定义__str__() print的时候得不到自己想要的东西 类默认转化的字符串基本没有我们想要的一些东西,仅仅包含了类的名称以及实例的 ID (理解为 Python 对象的内存地址即可).虽 ...

  7. isinstance,issubclass,内置函数__str__和__repr__,__format__,dir()函数

    isinstance(obj,cls) 检查是否obj是否是类 cls 的对象 #对象与类之间的关系 判断第一个参数是否是第二个参数的实例 # 身份运算 # 2 == 3 # 值是否相等# 2 is ...

  8. 9.4、__del__、__doc__、__dict__、__module__、__getitem__、__setitem__、__delitem__、__str__、__repr__、__call__

    相关内容: __del__.__doc__.__dict__.__module__.__getitem__.__setitem__.__delitem__.__str__.__repr__.__cal ...

  9. python 全栈开发,Day24(复习,__str__和__repr__,__format__,__call__,__eq__,__del__,__new__,item系列)

    反射: 使用字符串数据类型的变量名来使用变量 wwwh即what,where,why,how  这4点是一种学习方法 反射 :使用字符串数据类型的变量名来使用变量 1.文件中存储的都是字符串 2.网络 ...

随机推荐

  1. Linux内核分析-系统中断在内核中的实现

    分析system_call中断处理过程 在MenuOS中添加上周所运用到的系统调用 即在Linuxkernel/menu/test.c文件中,添加代码如下: int Mkdir() { const c ...

  2. 『编程题全队』Beta 阶段用户使用调查报告

    目录 一.项目概述 1.1项目名称 1.2项目简介 1.3项目预期达到目标 1.4项目测试方法 二.项目测试过程 2.1测试对象 2.2测试时长 2.3用户测试反馈 一.项目概述 1.1项目名称 本次 ...

  3. What is the difference between apache tomcat deployer and core version? - Stack Overflow

    java - What is the difference between apache tomcat deployer and core version? - Stack Overflowhttps ...

  4. IP config with netsh under windows

    ================================================= CHINAUNICOME.bat route delete 0.0.0.0 mask 0.0.0.0 ...

  5. logstash 使用kafka范例

    写入到kafka input { stdin { } } output { kafka { bootstrap_servers => "10.0.0.200:9092" to ...

  6. python之json数据存储

    # 数据存储:json.dump()和json.load() # date:2017-07-17 import json file_name = 'D:/json_file.txt' nums = [ ...

  7. KEYENCE Programming Contest 2019 自闭记

    A:签到. #include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> ...

  8. UVa - 10339

    It has been said that a watch that is stopped keeps better time than one that loses 1 second per day ...

  9. itexpdf同一个段落不同文字,如何设置不同的格式

    Java使用itexpdf生成PDF,正常情况下,新建一个段落Paragraph,然后可以给段落添加一个格式BaseFont Paragraph paragraphBlue = new Paragra ...

  10. MT【205】寻找对称中心

    函数$f(x)=\dfrac{x}{x+1}+\dfrac{x+1}{x+2}+\cdots+\dfrac{x+2018}{x+2019}$ 的图像的对称中心_____ 提示:根据定义域可知如果有对称 ...