__str__ 以及 __repr__
code
class mytest(): def __str__(self): return "hello" def __repr__(self): return "world" a=mytest() print(a) print(str(a)) print(repr(a))
outputs
macname@MacdeMBP Desktop % python3 test_0823.py
hello
hello
world
macname@MacdeMBP Desktop %