Neil G
Neil G

Reputation: 33202

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?

Upvotes: 6

Views: 1255

Answers (1)

ecatmur
ecatmur

Reputation: 157344

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

Upvotes: 9

Related Questions