I know that methods __repr__
and __str__
exist to give a formal and informal representation of class instances. But does an equivalent exist for class objects too, so that when the class object is printed, a nice representation of it could be shown?
>>> class Foo:
... def __str__(self):
... return "instance of class Foo"
...
>>> foo = Foo()
>>> print foo
instance of class Foo
>>> print Foo
__main__.Foo
When you call
print(foo)
,foo
's__str__
method is called.__str__
is found in the class offoo
, which isFoo
.Similarly, when you call
print(Foo)
,Foo
's__str__
method is called.__str__
is found in the class ofFoo
, which is normallytype
. You can change that using a metaclass: