Hacker News new | ask | show | jobs
by diarrhea 1630 days ago
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.

1 comments

> 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.

[1]: https://web.archive.org/web/20170805220114/http://www.cafepy...

Is this definition recursive? Meaning that classes whose instances are metaclasses are also metaclasses?
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.

Check the type of `type` in Python ;-)