Ravindu Shantha Ravindu Shantha - 17 days ago 5
Python Question

What Can be instantiation from a Metaclass

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

__init__
,
__del__
,
__call__
come from the default metaclass
type
. Is this correct?

Answer

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()
1
Comments