当未定义 __str__、__repr__ 或 __format__ 的类型转换为字符串时,进行报告。 默认的字符串表示形式可能不适用于调试或显示目的。
示例:
class A:
pass
str(A()) # 结果:“<__main__.A object at 0x...>”
请考虑定义下列一种方法,以提供有意义的字符串表示:
class A:
def __str__(self):
return "A instance"
def __repr__(self):
return "A()"