【人工智能概述】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
相关推荐
小小测试开发1 小时前
安装 Python 3.10+
开发语言·人工智能·python
梦想不只是梦与想2 小时前
Python 中的装饰器
python·装饰器
我叫唧唧波2 小时前
Python+AI 全栈学习笔记
人工智能·python·学习
AAA大运重卡何师傅(专跑国道)3 小时前
【无标题】
开发语言·c#
copyer_xyf3 小时前
Python 异常处理
前端·后端·python
XBodhi.3 小时前
Visual Studio C++ 语法错误: 缺少“;”(在“return”的前面)
开发语言·c++·visual studio
麻雀飞吧3 小时前
期货多合约策略目标持仓怎么更新才不乱
python·区块链
Cthy_hy3 小时前
拓扑排序超详解:原理 + Kahn 贪心算法
python·算法·贪心算法
LSssT.3 小时前
【01】Python 机器学习
开发语言·python
为爱停留4 小时前
给智能体装上「刹车」:中断(Interrupts)与人工审批全解析
python