【人工智能概述】python妙用 __str__()

【人工智能概述】python妙用 str()

文章目录


一.python内置函数__str__()

  • 通过自定义__str__()函数可以打印对象中相关的内容。
python 复制代码
class Person(object):
    def __init__(self, name = 'tom', age = 10):
        self.name = name
        self.age = age
        self.parent = None
    
    def __str__(self):
        return "Your name is: " + self.name
    
tom = Person()
print(tom)
 
# 唔,果然输出了我们自定义的内容
# >>> Your name is: tom
相关推荐
Wang's Blog39 分钟前
Go-Zero 项目开发43:基于 Filebeat 收集各个服务的日志信息
开发语言·golang·go-zero·filebeat
丈剑走天涯2 小时前
JDK 17 正式特性
java·开发语言
秋田君2 小时前
QT_QFontDialog类字体对话框
开发语言·qt
圣光SG2 小时前
Java操作题练习(七)
java·开发语言·算法
_Jimmy_3 小时前
Agent 溯源精度提升方案
人工智能·python·langchain
麻瓜老宋3 小时前
AI开发C语言应用按步走,表达式计算器calc的第二十三步,多行输入、进制输出、错误恢复、常量折叠、配置加载等
c语言·开发语言·atomcode
q567315234 小时前
企业级 HTTP 代理采购选型:技术评估清单 15 项
开发语言·网络·爬虫·网络协议·http·隧道ip·代理ip
SamChan904 小时前
在Web应用中集成PDF多语言翻译功能:PDFTranslator API实战指南
前端·python·ai·pdf·yapi·机器翻译
天天进步20154 小时前
Python全栈项目--智能办公自动化系统
开发语言·python
颜酱5 小时前
09 | 重构项目结构
人工智能·python·langchain