Python中自定义类如果重写了__repr__方法为什么会影响到str的输出?
2024-09-02 09:34:19
这是因为Python3中,str的输出是调用类的实例方法__str__来输出,如果__str__方法没有重写,则自动继承object类的__str__方法,而object类的__str__方法是调用__repr__方法,因此自定义类未重写__str__方法的情况下,实际上就是调用该类的__repr__方法,因此__repr__方法重写会影响到str的输出,除非在自定义类中也重写__str__方法才可以单独输出信息。
具体可参考:
《Python __repr__方法和__str__方法、内置函数repr和str的异同点对比剖析》
《Python重写自定义类的__repr__方法》
《 Python重写自定义类的__str__方法》
老猿Python,跟老猿学Python! 博客地址:https://blog.csdn.net/LaoYuanPython
最新文章
- android开发--Application
- linux 设置mysql 数据库编码utf8
- EditPlus开发Python的简单设置
- 与你相遇好幸运,Sails.js自定义responses
- spring文件下载记录
- centos搭建NFS网络文件系统
- linux包之procps之ps与top
- C# 获取图片的EXIF 信息
- Android开发之定义接口暴露数据
- Spring MVC使用样例
- Building Block
- 使用lrucache和diskLrucache实现照片墙
- Java常用类库 读书笔记 二
- Day 4-6 xml处理
- bootcdn
- hybrid几种模式
- Linux笔记(一):CentOS-7安装
- POJ 2049— Finding Nemo(三维BFS)10/200
- 进阶之路(基础篇) - 011 arduino api基础手册
- [BUAA OO]第一次博客作业