One use is for e.g. binding of database models for an ORM.
class FooModel(metaclass=Base):
x = Field(type=int, primary_key=True)
y = Field(type=reference(BarModel))
The Base metaclass will set it up to implement methods like save() by inheriting from parent classes, but it would also be nice for the library to have a list of all model types without the library user having to call a method like FooORM.register_type(FooModel). So the metaclass is being used in these classes to build up a dictionary of models when the class definition is encountered.
The metaclass is basically a class that itself builds classes, which means it can be syntactically convoluted.
However, with __init_subclass__ you can write a thing that looks like a regular class with regular parent methods, but instead just gets a method called each time the interpreter encounters a new subclass, which lets you do things like build up that dictionary for your ORM.
What classes are to instances, metaclasses are to classes.
This is from someone who has only ever read about, never used metaclasses, because they are widely regarded similar to git submodules. If you cannot really assert that you need them, you don't. They solve very specific problems, mostly found in library, not user code. A library can then allow user classes to be modified comprehensively. If you control the classes in the first place (not library code), you probably can do without metaclasses.
> What classes are to instances, metaclasses are to classes.
Given the popularity of this construct for analogies and metaphorical comparisons, it should be noted that this is strictly literal. In Python classes are objects, and the classes whose instances are classes are called “metaclasses” (and they are subclasses of the class “type”.)
Parent and grandparent comments and their siblings provide a good summary of metaclasses. One of the best deep dives I’ve seen is this document [1] which is offline but still available through the Wayback Machine.
GP is right that metaclasses are rarely used and you’ll kind of know when you need them.
Because metaclasses are classes, yes, classes whose instances are metaclasses are also classes whose instances are classes and thus are also metaclasses. But you could distinguish this subset of metaclasses as “metametaclasses” if you wanted to, to distinguish them from more general metaclasses just as metaclasses are distinguished from more general classes.
But AFAIK no one has come up with a distinct application for custom metametaclasses which would make having terminology to discuss them necessary or useful other than for entertainment.
The metaclass is basically a class that itself builds classes, which means it can be syntactically convoluted.
However, with __init_subclass__ you can write a thing that looks like a regular class with regular parent methods, but instead just gets a method called each time the interpreter encounters a new subclass, which lets you do things like build up that dictionary for your ORM.