Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
A metaclass is the class of a class and controls how classes are created; most use cases, such as registration or validation of subclasses, are better served by __init_subclass__, class decorators or descriptors.
Classes are created by calling their metaclass, which is type by default, with the class name, bases and namespace. A custom metaclass can modify this process, which is how ORMs historically turned field declarations into database mappings and how ABCMeta enforces abstract methods. Metaclasses are powerful but make code harder to understand, and combining two libraries that use different metaclasses causes conflicts. init_subclass runs whenever a subclass is defined and handles plugin registration or checking required attributes, class decorators can transform a single class explicitly, and set_name on descriptors lets fields know their attribute names. Reach for a metaclass only when you need to control class creation itself, such as customizing the namespace.