You need to define __repr__ on the metaclass.
class Meta(type):
def __repr__(cls):
return 'My class %s' % cls.__name__
class A(object):
__metaclass__ = Meta
__repr__ returns a representation of an instance of an object. So by defining __repr__ on A, you're specifying what you want repr(A()) to look like.
To define the representation of the class, you need to define how an instance of type is represented. In this case, replace type with a custom metaclass with __repr__ defined as you need.
>> repr(A)
My class A
If you want to define a custom __repr__ for each class, I'm not sure there's a particularly clean way to do it. But you could do something like this.
class Meta(type):
def __repr__(cls):
if hasattr(cls, '_class_repr'):
return getattr(cls, '_class_repr')()
else:
return super(Meta, cls).__repr__()
class A(object):
__metaclass__ = Meta
@classmethod
def _class_repr(cls):
return 'My class %s' % cls.__name__
class B(object):
__metaclass__ = Meta
Then you can customize on a per-class basis.
>> repr(A)
My class A
>> repr(B)
<__main__.B object at 0xb772068c>