>>> 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. 机器学习——KNN

    导入类库 import numpy as np from sklearn.neighbors import KNeighborsClassifier from sklearn.model_select ...

  2. Android Studio帮助文档的安装及智能提示设置

    初次使用Android Studio,发现其智能提示不能像Visual Studio一样显示系统方法等的详细用途描述.经查找资料,问题原因是未安装SDK Document. 解决办法如下: 1.打开如 ...

  3. lstm bptt推导

    深蓝 nlp 180429这个有详细的讲解

  4. 马昕璐201771010118《面向对象程序设计(java)》第七周学习总结

    第一部分:理论知识学习部分 Java用于控制可见性的4个访问权限修饰符: 1.private(只有该类可以访问) 2.protected(该类及其子类的成员可以访问,同一个包中的类也可访问) 3.pu ...

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

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

  6. [LeetCode] Design Linked List 设计链表

    Design your implementation of the linked list. You can choose to use the singly linked list or the d ...

  7. sql的基础用法

    # sql 对大小写不敏感 # 查询表中的所有信息 select * from `Customers`; # 查询指定字段 CustomerName,Country select CustomerNa ...

  8. ubuntu16.04 anaconda的安装和卸载

    第一次安装: 1.直接从官网下载了anaconda安装包,然后bash ...sh安装. 2.过程中主要需要选择安装路径,为了把安装的软件都放在一起,我新建了一个install_software在系统 ...

  9. php 获取 两个时间戳之间 相隔 【多少年】 【 多少个月】 【多少天】 【 多少个小时】 【多少分】【 多少秒 】

    /** * 返回两个时间的相距时间,*年*月*日*时*分*秒 * @param int $one_time 时间戳一 大的时间戳 * @param int $two_time 时间戳二 小的时间戳 * ...

  10. 滑动viewpage

    Adapter: package com.example.fashionyuan.Adatader; import android.support.v4.app.Fragment;import and ...