__str__:
在调用__str__的时候,是在调用print函数,打印实例的时候自动调用.
class Test:
def __init__(self,name="jack",age=20):
self.name = name
self.age = age
class TestStr(Test):
def __repr__(self):
return "__repr__:%s:%s"%(self.name,self.age)
def __str__(self):
return "__str__:%s:%s"%(self.name,self.age)
t = TestStr()
print(t)
F:\学习代码\Python代码\venv\Scripts\python.exe F:/学习代码/Python代码/day5/运算符重载.py
__str__:jack:20
Process finished with exit code 0
__repr__:
__repr__和__str__同时存在的时候,__repr__ = __str__.
__repr__在交互模式下输入实例,当按下回车的时候回自动调用.
>>> class Test:
... def __init__(self,name="jack",age=18):
... self.name = name
... self.ag