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()) #相当 ...
随机推荐
- STS的安装以及IDEA安装和破解过程
一.STS的下载 1·下载地址:直接百度搜索STS,选择Download STS 3 2.选择电脑对应的版本 直接下载 3.安装包解压后在有jdk的情况下就可以直接使用 二.IDEA的安装 下载网址: ...
- Java_并发工具包 java.util.concurrent 用户指南(转)
译序 本指南根据 Jakob Jenkov 最新博客翻译,请随时关注博客更新:http://tutorials.jenkov.com/java-util-concurrent/index.html.本 ...
- Java之为何配置环境变量
一.不要问一个程序员为什么命名是基础零(^_^) 二.Java为什么跨平台: 因为有java虚拟机,一个程序的运行必然要依赖于系统,java的跨平台是因为java虚拟机jvm把不同平台编写的代码编译成 ...
- editplus注册码生成
http://www.jb51.net/tools/editplus/ 主要JS代码: function generate_editplus_regcode() { var list = [0,493 ...
- map映射
采集于:https://blog.csdn.net/luanpeng825485697/article/details/78056312 映射map: var map = new Map(); //映 ...
- Python Learning - Two
1. Built-in Modules and Functions 1) Function def greeting(name): print("Hello,", name) g ...
- linux安装杀毒软件
https://www.cnblogs.com/bingo1024/p/9018212.html
- 如何设置 jenkins 任务执行的历史记录在左侧显示的格式?
java -jar I:\CDC\jenkins\jenkins-cli.jar -s http://$ENV:MasterHost.us.oracle.com set-build-display-n ...
- Python: map() and reduce()
map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 举例说明,比如我们有一个函数f(x)=x2,要把这个函数作用在一个lis ...
- c++ 库函数cmath
cmath中常用库函数: int abs(int i);//返回整型参数i的绝对值double fabs(double x);//返回双精度参数x的绝对值long labs(long n);//返回长 ...