reference:
Declaring a class is equivalent to creating an instance of the type class.
class C: ... ≡ C = type('C', ...)
reference:
It is the metaclass
specification that customizes this type instantiation. You can customize class generation when declaring a class by declaring the metaclass M
as a subclass of type
and overriding the __new__
method:
class C(metaclass=M): ...
We speculate that it is not possible to force M
to be applied when all classes are generated [investigation required].
reference:
Customization of instantiation is done by defining (overriding) the classes __new__
and __init__
.
In particular, by customizing __new__
, it is possible to generate and return a class instance other than the class intended to be generated (C
of c = C (...)
). This can be used, for example, to implement factory classes.
On the other hand, as you know, __init__
is used to initialize the created instance (set properties, etc.).
__prepare__
I will investigate this time.
Recommended Posts