>>> 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. Sting、StringBuffer、StringBuilder

    (1)String是字符串常量,一旦创建之后不可更改:StringBuffer和StringBuilder是字符串变量,可以更改.String的不可变,所以适合作为Map的键. (2)StringBu ...

  2. PHP文件上传大小限制问题

    一.Thinkphp方面限制  $upload->maxSize   =     31457280 ; //设置附件上传大小 二.七牛方面限制: 'UPLOAD_FILE_QINIU' => ...

  3. python飞机大战代码

    import pygame from pygame.locals import * from pygame.sprite import Sprite import random import time ...

  4. sql基本

    SELECT: select * from table select 列名 from table select DISTINCT 列名 from table INSERT: insert into t ...

  5. eclipse设置properties文件的字体颜色

    点击Window->preferences->搜素properties ============================ 其它设置字体颜色设置        =========== ...

  6. 微信小程序 fixed 解决 textarea 的 placeholder 不固定问题

    当我们把 textarea 放到一个 position:fixed; 中的元素中时. 会发现这个 textarea 也会跟着固定位置,但是 textarea 的 placeholder 内容不会固定, ...

  7. 壁虎书7 Ensemble Learning and Random Forests

    if you aggregate the predictions of a group of predictors,you will often get better predictions than ...

  8. js 对象属性遍历

    function 对象属性遍历(){ var obj = {x:1,y:2,c:3};for (var name in obj){ alert ( obj[name] )} } function 数组 ...

  9. Mac学习

    碰到问题可以多查看帮助文件 快速上手 1,自动隐藏顶端菜单栏: 通用-> 外观 menu bar 2,docker 程序坞,左边为应用程序,右边是文件或者文件夹,38线 3,option -&g ...

  10. robotframework RF使用中需要安装的工具和库

    确保 Python 3.6.2 安装成功 安装 如下 RF使用中需要的工具和库 1. RF 在两个Python中安装 robotframework执行命令 pip install robotframe ...