In Python, a class has 2 methods that can return a string representation, __str__()
and __repr__()
.
__str__()
is called by the format()
and print()
functions. It should return a nicely printable representation of the object.
The default implemntation of __str__()
in class object
just calls __repr__()
.
__repr__()
is meant for debuggers, and should return an informative and unambiguous representation of the object. Ideally, it should take the form of a valid Python expression, such as a constructor call, that can be used to reconstruct the object. You can see this representation by using the built-in repr()
function on an object.