- '''
- __str__(): 当打印对象名的时候自动执行
- 要求: 必须 return 字符串
- '''
- class Person:
- def __init__(self):
- self.name = None
- self.age = None
- def __str__(self):
- return "姓名:%s, 年龄:%d"%(self.name,self.age)
- p=Person()
- p.name="张三"
- p.age=10
- print(p.name,p.age)
- print(p)
- '
来源: http://www.bubuko.com/infodetail-2892968.html