pythonpython-3.xmetaclass

Determine a class's metaclass in Python 3


In Python 2, I could check a class's __metaclass__ attribute to determine its metaclass.

How do I the same thing in Python 3?


Solution

  • Use the single-argument type function (type(class)), or just access class.__class__. Both of these work in Python 2, btw.

    E.g.,

    In [4]: class MyMetaclass(type): pass
    
    In [5]: class MyClass(metaclass=MyMetaclass): pass
    
    In [6]: type(MyClass)
    Out[6]: __main__.MyMetaclass