Override __repr__
.
>>> class Foo: pass
...
>>> print(Foo())
<__main__.Foo object at 0x7f7296192da0>
>>> class Foo:
... def __repr__(self):
... return "Foo"
...
>>> print(Foo())
Foo
Note that converting to an object to a string will call __str__
first and then fall back to __repr__
>>> class Foo:
... def __repr__(self):
... return "Foo"
... def __str__(self):
... return "Foo (string)"
...
>>> f"{Foo()}"
'Foo (string)'
>>> str(Foo())
'Foo (string)'
>>> Foo()
Foo
You may also wish to read How can I choose a custom string representation for a class itself (not instances of the class)? for information on changing the representation of a class as opposed to instances of that class.