作用:显示类的属性,便于我们在打印类名的
class test:
def __init__(self):
self.intro = "this is test"
self.age = "123"
def __repr__(self):
print("hello")
return "name:"+ self.intro +",age:" + self.age
t = test()
print(test())
#输出
hello
name:this is test,age:123
注意这里__repr__方法一定要返回一个非空字符串,否则会报错
参考:C语言中文网