>>> 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. HDU 2008 数值统计

    题目链接:HDU 2008 Description 统计给定的n个数中,负数.零和正数的个数. Input 输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的 ...

  2. pytorch简介

    诞生 1.2017年1月,Facebook人工智能研究院(FAIR)团队在GitHub上开源了pyTorch,并迅速占领GitHub热度榜榜首. 常见深度学习框架简介 Theano 1.Theano最 ...

  3. ECMA Script 6_ 类 class

    类 class ES6 提供了更接近传统语言的写法,引入了 Class(类)这个概念,作为对象的模板. 通过 class 关键字,可以定义类 class 新的 class 写法只是让对象原型的写法更加 ...

  4. [LeetCode] To Lower Case 转为小写

    Implement function ToLowerCase() that has a string parameter str, and returns the same string in low ...

  5. 基于贝叶斯算法实现简单的分类(java)

    参考文章:https://blog.csdn.net/qq_32690999/article/details/78737393 项目代码目录结构 模拟训练的数据集 核心代码 Bayes.java pa ...

  6. ad 线束和网络

    ad 线束和通道复用会遇到网络无法更新的pcb的异常, 就算,我们把网络作用范围调整到页,电源全局仍然会失败,通过查找原因 尽量网络标签不同.稍微更改就可以 比如  sda1,sda1s不要一样

  7. JAVAjdk第一次作业

    第一次的作业由于对JAVA程序的不熟悉以及jdk环境变量的配置问题,第一次的作业写了很久

  8. ublox TMOD2

    Survey_in模式就是为了当满足下面的条件的时候,自动跳转到fixmod

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

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

  10. Xcode工程编译错误之iOS开发之The Xcode build system has crashed. Please close and reopen your workspace

    解决方法: . 删除DerivedData . 参照上面的链接设置:File -> Workspace Settings -> Build System -> Legacy Buil ...