I have a Python class with class attributes, and I would like it to show the values of the attributes when printed.
But instead, it just shows the name of the class.
class CONFIG:
ONE = 1
TWO = 2
print(CONFIG) # <class '__main__.CONFIG'>
Of course, if I wanted to instantiate the class, then I could def __repr__()
and control how it prints.
The question is, how do I control how a class (NOT a class instance) prints? Is there an equivalent to __repr__
for the class itself?
Workarounds:
- Write a function that uses
vars()
ordir()
to iterate over the attributes - Instantiate the class, stop doing things a weird way :)
- Change my code in some other way so I no longer have the requirement to print the class (use Enum, SimpleNamespace, etc.)