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()) #相当 ...
随机推荐
- SDOI2018:原题识别
题解: https://files.cnblogs.com/files/clrs97/old-solution.pdf Code: #include<cstdio> #include< ...
- srand rand
#include <stdlib.h> srand( (time(0)); rand()%100;
- 201771010126 王燕《面向对象程序设计(Java)》第七周实验总结
实验七 继承附加实验 实验时间 2018-10-11 1.实验目的与要求 (1)进一步理解4个成员访问权限修饰符的用途: private--私有域或私有方法:只能在定义它的类中使用 public--公 ...
- 关于 vuex 的使用忠告
第一.看明白这张图在说话 简单解释一下,actions接收到components的行为后actions请求api 等获取数据,提交到mutations,然后mutations中才改变state ,反映 ...
- String对象方法属性总结
常用属性: constructor;length;prototype;(不在解释): 常用方法: charAt(index);返回指定位置的字符. concat(stringX);连接字符串. ind ...
- 快速体验 Laravel 自带的注册、登录功能
快速体验 Laravel 自带的注册.登录功能 注册.登录经常是一件很伤脑筋的是,Laravel 提供了解决方案,可以直接使用它.试过之后,感觉真爽! 前提:本地已安装好了 PHP 运行环境.mysq ...
- 旧版本的firefox 下载 和 安装(查找web元素路径) ---web 元素 自动化测试
ftp.mozilla.orgpubfirefoxreleases 旧版下载地址 选择47版本 因为48后面的会进行插件校验 这样firepath安装不成功 安装文件:在百度 ...
- [05-02]红帽linux常用操作命令
命令怎么用(三种方式) shutdown --help shutdown --? man shutdown (man 就是manual 手册, 指南) 服务 service 怎么知道服务的名字呢? ...
- kali2.0安装docker(转)
开始部署 1. Docker需要Linux Kernels 大于3.10并且是64-bit的机器,用uname -a可以查看是否符合要求. 2. 执行命令编辑文本: vim /etc/apt/sour ...
- ACL(Access Control List)
一.ACL的简介 ACL(Access Control List 访问控制列表)是路由器和交换机接口的指令列表,用来控制端口进出的数据包.ACL的定义也是基于每一种被动路由协议的,且适用于所有的被动路 ...