["python","class"]
How can I choose a custom string representation for a class itself (not instances of the class)?

Consider this class: class foo(object): pass The default string representation looks something like this: >>> str(foo) "<class '__main__.foo'>" How can I make this display a custom string? See How to print instances of a class using print()? for the corresponding question about instances of the class. In fact, this question is really a special case of that one - because in Python, classes are themselves also objects belonging to their own class - but it's not directly obvious how to apply the advice, since the default "class of classes" is pre-defined.
Implement __str__() or __repr__() in the class's metaclass. class MC(type): def __repr__(self): return 'Wahaha!' class C(object): __metaclass__ = MC print(C) Use __str__ if you mean a readable stringification, use __repr__ for unambiguous representations. Edit: Python 3 Version class MC(type): def __repr__(self): return 'Wahaha!' class C(object, metaclass=MC): pass print(C)