Python __str__函数,,class Cat:
Python __str__函数,,class Cat:
class Cat: def __init__(self,_name): self.name = _name def __str__(self): return "i am %s"%self.name def show(self): print("name is %s"%self.name)tom = Cat("tom")tom.show()print(tom)print("------------");lanmao = Cat("lanpang")lanmao.show()print(lanmao)
__str__函数要求有返回值,必须有return,__str__函数功能是将对象字符串化
Python __str__函数
评论关闭