__str__ Python · 特殊方法
定义对象被 print 或 str() 时显示的字符串。
怎么用
def __str__(self):
小例子
class 猫:
def __str__(self):
return '一只猫'
print(猫())
运行结果
一只猫
官方文档:https://docs.python.org/zh-cn/3/reference/datamodel.html#object.__str__
在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条