String representation of a class in Python 3

I would like my classes to have a string representation based on a class variable (which can be different in derived classes). This answer suggests that the metaclasses could be as follows:

class MC(type):
    def __repr__(self):
        return 'Wahaha!'

class C():
    __metaclass__ = MC

print(C)

      

But that doesn't work in Python 3, returning

<class '__main__.C'>

      

instead of Wahaha!

. Can someone explain to me what changed between Python 2 and 3 and how to do it in Python 3?

+3


source to share


1 answer


What has changed since the metaclass was declared in 3.x.



class C(metaclass=MC):
    pass

      

+5


source







All Articles