To change the presentation of a class string:
class MC(type): def __repr__(cls): return 'I am Test' class Test: __metaclass__ = MC pass print Test
works great.
If repr(Test) is called when you define __str__ , it will not use your custimized message.
However, if you define __repr__ as I do and str(Test) is called, it will use your custimized message because __repr__ is fallback and __str__ not defined in type .
If all you want to do is change its name:
def renamer(name): def wrapper(func): func.__name__ = name return func return wrapper @renamer('Not Test') class Test: pass print Test.__name__ Test.__name__ = 'Test Again' print Test.__name__
both will work to change the class name.
source share