Ravindu Shantha Ravindu Shantha - 1 year ago 60
Python Question

What Can be instantiation from a Metaclass

What can a class instantiation from metaclass? As I understand it, usually methods like

come from the default metaclass
. Is this correct?

Answer Source

We should not talk about inheritance but instantiation. Class is related to its metaclass in the same way how instance is related to its base class. Class is an instance of metaclass.

In particular every field defined on a metaclass is accessible from the class as a "metaclass field". So if one metaclass is used for multiple classes then fields on the metaclass are shared between those classes.

Note that __init__ on metaclass is not the same as __init__ on class. These are unrelated. Also __call__ becomes a new "constructor". Check this out:

>>> class Meta(type):
...     def __call__(self):
...         return 1

>>> class Foo(metaclass=Meta):
...     pass

>>> Foo()