Hacker News new | ask | show | jobs
by pengwing 1621 days ago
I feel jumping right into __init_subclass__ without explaining why metaclasses exist and what problems they typically solve excludes the majority of devs on HN. Thereby limiting any discussion only to the advanced Python developer echo chamber, while discussion across very different devs is usually far more interesting.
5 comments

Note that this doesn't appear to have been submitted by the post's author, and HN may not have been the intended audience.
Simon keeps his TIL (today I learned) blog as a way to take notes mainly for himself, from what I understand.

I like the way he documents everything in Github issues[0]. I learn a new thing or two with every release of Datasette, because there's so much troubleshooting full of relevant links and solutions. Same with his TIL blog.

[0]: https://github.com/simonw/datasette/issues/1576

I've not seen this before but it's so cool! I've lost track of all the ways I've tried to keep track of things I've learned. Maybe this would be a good way to do it.

Link to the GitHub repo for those interested: https://github.com/simonw/til

>I feel jumping right into __init_subclass__ without explaining why metaclasses exist

i have a text on the python object system and metaclasss here https://github.com/MoserMichael/python-obj-system/blob/maste... as part of my free python course https://github.com/MoserMichael/python-obj-system/blob/maste... (didn't cover __init_subclass__ yet, will have to extend it. You never stop learning with python...)

Could you, or someone else, clue us in?

Edit: another user linked their blog https://news.ycombinator.com/item?id=29813349

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.

[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 ;-)
Where is this advanced Python echo chamber?

I’m a pretty good Python dev, but I feel in order to advance my skills I need to get into extensions … which I don’t have the time for.

Any suggestions on advanced Python resources?

Check out these books:

* Fluent Python — takes you through Python’s core language features and libraries, and shows you how to make your code shorter, faster, and more readable at the same time

* Serious Python — deployment, scalability, testing, and more

* Practices of the Python Pro — learn to design professional-level, clean, easily maintainable software at scale, includes examples for software development best practices

Fluent Python 2nd edition was just released in December, but I can unfortunately not find it anywhere
Author mentioned physical copies to be available around April.
I didn’t even know it was Python, I guessed it was something C#