I understand the following Python code:
>>> class A(object):
... def __str__(self):
... return "An instance of the class A"
...
>>>
>>> a = A()
>>> print a
An instance of the class A
Now, I would like to change the output of
>>> print A
<class '__main__.A'>
Which function do I need to overload to be able to do that? The solution has to work even if the class is never instantiated. Is the situation different in Python 2.x and 3?
Define
__str__()on the metaclass:Now,
print Awill printplonk.Edit: As noted by jsbueno in the comments, in Python 3.x you would need to do the following:
Even in Python 2.x it might be a better idea to define the metaclass outside the class body — I chose the nested form above to save some typing.