the difference __str__ and __repr__
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__的更多相关文章
- 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/ 目的 ...
- python中__str__与__repr__的区别
__str__和repr __str__和__repr__都是python的内置方法,都用与将对象的属性转化成人类容易识别的信息,他们有什么区别呢 来看一段代码 from math import hy ...
- python 的特殊方法 __str__和__repr__
__str__和__repr__ 如果要把一个类的实例变成 str,就需要实现特殊方法__str__(): class Person(object): def __init__(self, name, ...
- __str__与__repr__
在讲解之前,我们先来了解下str和repr的区别:两者都是用来将数字,列表等类型的数据转化为字符串的形式.不同之处在于str更加类似于C语言中使用printf输出的内容,而repr输出的内容会直接将变 ...
- python之反射和内置函数__str__、__repr__
一.反射 反射类中的变量 反射对象中的变量 反射模块中的变量 反射本文件中的变量 .定义:使用字符串数据类型的变量名 来获取这个变量的值 例如: name = 'xiaoming' print(nam ...
- python __str__() 和 __repr__()是干啥的
1. 没定义__str__() print的时候得不到自己想要的东西 类默认转化的字符串基本没有我们想要的一些东西,仅仅包含了类的名称以及实例的 ID (理解为 Python 对象的内存地址即可).虽 ...
- isinstance,issubclass,内置函数__str__和__repr__,__format__,dir()函数
isinstance(obj,cls) 检查是否obj是否是类 cls 的对象 #对象与类之间的关系 判断第一个参数是否是第二个参数的实例 # 身份运算 # 2 == 3 # 值是否相等# 2 is ...
- 9.4、__del__、__doc__、__dict__、__module__、__getitem__、__setitem__、__delitem__、__str__、__repr__、__call__
相关内容: __del__.__doc__.__dict__.__module__.__getitem__.__setitem__.__delitem__.__str__.__repr__.__cal ...
- python 全栈开发,Day24(复习,__str__和__repr__,__format__,__call__,__eq__,__del__,__new__,item系列)
反射: 使用字符串数据类型的变量名来使用变量 wwwh即what,where,why,how 这4点是一种学习方法 反射 :使用字符串数据类型的变量名来使用变量 1.文件中存储的都是字符串 2.网络 ...
随机推荐
- Daily Scrum 12-25
Meeting Minutes 针对设计师提出的问题完成了layout的微调: 讨论alpha测试反馈反映出的一些问题: 完成了代码的merge(与bing词典 1.5版本): Progress ...
- 我的software
每个学计算机软件的同学都有可能经历以下的情况: 1. 哎,我家电脑开不了机了,来帮帮忙 2. 我耳机坏了,你给修修吧 3. 你能换手机屏不 4. 过来看下,我的Word打不开了 等等等等 这些 ...
- The Golden Age CodeForces - 813B (数学+枚举)
Unlucky year in Berland is such a year that its number n can be represented as n = xa + yb, where a ...
- eclispe file查找
今天查找一段js代码时在本页内找不到,所以需要在整个工程下寻找. 过程如下
- 正则表达式(java)
概念: 正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念. 正则表通常被用来检索.替换那些符合某个模式( ...
- Maven修改默认JDK
Maven修改默认JDK 问题: 1.创建maven项目的时候,jdk版本是1.5版本,而自己安装的是1.7或者1.8版本. 2.每次右键项目名-maven->update project 时候 ...
- Solution of wireless link "PCI unknown" on Centos 7.1
Pick From http://www.blogjava.net/miaoyachun/archive/2015/09/17/427366.html After Centos 7.1 tobe in ...
- mysql复杂查询1
https://blog.csdn.net/fly910905/article/details/79846949
- 关于RabbitMQ Queue Argument的简介
1.Message TTL message在队列queue中可以存活多长时间,以毫秒为单位:发布的消息在queue时间超过了你设定的时间就会被删除掉. channel.queueDeclare(&qu ...
- codeforces431B
Shower Line CodeForces - 431B Many students live in a dormitory. A dormitory is a whole new world of ...