Hacker News new | ask | show | jobs
by dragonwriter 1626 days ago
> 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”.)

2 comments

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 ;-)