class类 __repr__ 与__str__
>>> class Student(object):
... def __init__(self, name):
... self.name = name
... def __str__(self):
... return 'Student object (name: %s)' % self.name
...
>>> s = Student('Michael')
>>> s
<__main__.Student object at 0x00000000022073C8>
>>> print(Student('Michael'))
Student object (name: Michael)
>>>
-------------------------------------------------------------------------------------------
没有写__str __()会调用 __repr__() 方法:
>>> class Student(object):
... def __init__(self, name):
... self.name = name
... def __repr__(self):
... return 'Student object (name: %s)' % self.name
...
>>> print(Student('Michael'))
Student object (name: Michael)
>>> s= Student('Michael')
>>> s
Student object (name: Michael)
>>>
在cmd下运行的Python3.6.5
说明:
用print()打印实例调用的是__str__()方法
不用print()打印实例调用的是__repr__()方法
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.
如果类定义__repr __()但不定义__str __(),那么当需要该类的实例的“informal”字符串表示时,也会使用__repr __()。
参考:
https://docs.python.org/3/reference/datamodel.html?highlight=__repr__#object.__repr__
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014319098638265527beb24f7840aa97de564ccc7f20f6000
https://www.cnblogs.com/aomi/p/7026532.html
class类 __repr__ 与__str__的更多相关文章
- python类中方法__str__()和__repr__()简单粗暴总结
在交互式模式下,类中同时实现__str__()和__repr__()方法: 直接输入实例名称显示repr返回的类容: 用print打印实例名称显示str返回的内容: >>> clas ...
- [转]Python中__repr__和__str__区别
class Test(object): def __init__(self, value='hello, world!'): self.data = value >>> t = Te ...
- Python中__repr__和__str__区别
Python中__repr__和__str__区别 看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): ...
- Python中__repr__和__str__区别(转)
class Test(object): def __init__(self, value='hello, world!'): self.data = value >>> t = Te ...
- __repr__与__str__
首先我们来举个例子,定义一个长方行类Cuboid,长为x,宽为y,高为z class Cuboid: def __init__(self, x = 3, y = 1, z = 2): self.x = ...
- Python面试题之Python中__repr__和__str__区别
看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): self.data = value >> ...
- Python3中__repr__和__str__区别
示例: class Test(object): def __init__(self, value='hello, world!'): self.data = value >>> t ...
- raindi python魔法函数(一)之__repr__与__str__
__repr__和__str__都是python中的特殊方法,都是用来输出实例对象的,如果没有定义这两个方法在打印的时候只会输出实例所在的内存地址 这种方式的输出没有可读性,并不能直观的体现实例.py ...
- python类中的__str__以及__repr__
一.__str__ 打印时触发 class A: def __str__(self): #打印时候执行的代码块 return 'ok' # 如果不返回字符串类型,则会报错 print(A()) #相当 ...
随机推荐
- 双接口(回调)promise cb
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [jzoj]2538.【NOIP2009TG】Hankson 的趣味题
Link https://jzoj.net/senior/#main/show/2538 Description Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫H ...
- Hibernate 双向一对多映射
附代码: public class Order { private Integer id; private String OrderName; private Customer customer; p ...
- Linux系统(和mac系统)chmod修改权限总结
在 Mac 系统的终端上修改文件权限使用的是 Linux 中的 chmod 命令. chmod 用户+操作+权限+文件 用户部分:使用字母 u 表示文件拥有者(user),g 表示拥有者所在群组(gr ...
- LCA(包含RMQ)
今天看了RMQ问题 ST的实质是动归 于是我来回顾一下LCA(的各种写法) 因为每次考试发现自己连LCA都写不好 费时 First of all, RMQ板子: [一维] #include<bi ...
- File类中的一些属性 添加删除文件夹
import java.io.File; import java.io.IOException; public class FileD { public static void main(String ...
- 剑指offer——python【第60题】把二叉树打印成多行
题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行.#类似于二维列表[[1,2],[4,5]] 解题思路 其实这倒题和其他类似的题有所区别,这里是分层打印,把每层的节点值放在同一 ...
- IIC稳定性.VBS
Sub Main Dim cnt Dim delay Dim time Dim atttime atttime = 20 delay = 3000 time = 50 crt.screen.Send ...
- mongoVUE破解与配置、Mongodb数据库安装
一.mongoVUE 1.5.3破解: 1) 开始-运行-regedit-搜索:B1159E65-821C3-21C5-CE21-34A484D54444 2.) 然后把1,2,3项数值删除,然后重新 ...
- CITS1401 Computational Thinking with Python
Department of Computer Science and Software EngineeringCITS1401 ComputationalThinking with PythonPro ...