Day_13_PM_str_and_repr
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
# 1. 必须返回字符串
# 2. 作用是让打印出来的对象的值是这里的返回值
def __str__(self): # 两个一起写优先使用str
return f'名字:{self.name},年龄:{self.age}'
# def __repr__(self):
# return f'名字2:{self.name},年龄2:{self.age}'
cat = Cat('汤姆', 3)
print(cat)
# => <__main__.Cat object at 0x0000000002122278>
# print(repr(cat))