>>> 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__的更多相关文章

  1. python类中方法__str__()和__repr__()简单粗暴总结

    在交互式模式下,类中同时实现__str__()和__repr__()方法: 直接输入实例名称显示repr返回的类容: 用print打印实例名称显示str返回的内容: >>> clas ...

  2. [转]Python中__repr__和__str__区别

    class Test(object): def __init__(self, value='hello, world!'): self.data = value >>> t = Te ...

  3. Python中__repr__和__str__区别

    Python中__repr__和__str__区别 看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): ...

  4. Python中__repr__和__str__区别(转)

    class Test(object): def __init__(self, value='hello, world!'): self.data = value >>> t = Te ...

  5. __repr__与__str__

    首先我们来举个例子,定义一个长方行类Cuboid,长为x,宽为y,高为z class Cuboid: def __init__(self, x = 3, y = 1, z = 2): self.x = ...

  6. Python面试题之Python中__repr__和__str__区别

    看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): self.data = value >> ...

  7. Python3中__repr__和__str__区别

    示例: class Test(object): def __init__(self, value='hello, world!'): self.data = value >>> t ...

  8. raindi python魔法函数(一)之__repr__与__str__

    __repr__和__str__都是python中的特殊方法,都是用来输出实例对象的,如果没有定义这两个方法在打印的时候只会输出实例所在的内存地址 这种方式的输出没有可读性,并不能直观的体现实例.py ...

  9. python类中的__str__以及__repr__

    一.__str__ 打印时触发 class A: def __str__(self): #打印时候执行的代码块 return 'ok' # 如果不返回字符串类型,则会报错 print(A()) #相当 ...

随机推荐

  1. LOJ 6270

    最近(一直)有点(很)蠢 按照区间大小排序做区间包含多少区间的话 只用考虑 左端点比当前左端点小的和右端点比当前右端点大的,因为不可能同时满足 关于K,就在做到K的时候减一下就好了,一直傻逼在这了 # ...

  2. QT杂记(网上资料整理而成)

    1.新建工程时,Qwidget和Qdialog和Qmianwindow三者的区别? QWidget是所有图形界面的基类QMainWindow是一个提供了菜单.工具条的程序主窗口QDialog是对话框. ...

  3. Python基础之语句1

    一.行定义(两类): 1.物理行:程序员编写代码的行. 2.逻辑行:python解释器需要执行的指令. 建议:一个逻辑行在一个物理行上,若一个物理行使用多个逻辑行,需要使用分号隔开:如果逻辑行过长,可 ...

  4. waitpid 函数详解

    关于Linux中waitpid函数的一些使用说明: #include<sys/types.h> #include<sys/wait.h> 定义函数 pid_t waitpid( ...

  5. 如何关闭git pull产生的merge 信息

    编辑 ~/.gitconfig [core] mergeoptions = --no-edit 或者终端之行 git config --global core.mergeoptions --no-ed ...

  6. js, Date.parse firefox 兼容

    Date.parse(dateVal); 这个方法很常用,parse() 方法可解析一个日期时间字符串,并返回 1970/1/1 午夜距离该日期时间的毫秒数. 可以验证输入日期是否窜在,不存在则返回N ...

  7. [Day25]IO(Properties、序列化流、打印流、Commons-IO)

    1.Properties类-持久的属性集,可保存在流中或从流中加载,属性列表中每个键及其对应值都是一个字符串 1.1 特点 (1)Hashtable的子类,map集合中的方法都可以用 (2)该集合没有 ...

  8. subline text3 安装 rem装换工具

    CSSREM 一个CSS的px值转rem值的Sublime Text 3自动完成插件. 插件效果如下: 安装 下载本项目,比如:git clone https://github.com/flashli ...

  9. poj1915

    #include<iostream> using namespace std; #define SIZE 305 int vis[SIZE][SIZE]; int sx,sy,ex,ey; ...

  10. Linux - ansible 安装

    # 安装依赖 yum install rpm-build python2-devel sshpass PyYAML python-jinja2 python-paramiko python-six p ...