Hacker News new | ask | show | jobs
by tybug 1628 days ago
The most salient example I can think of is tracking all subclasses of a class. This is how django tracks `Model` subclasses to create a table for each model, for instance. I wrote a blog post on this topic: https://tybug.github.io/2021/11/06/tracking-all-subclasses-o...

I've also used metaclasses in the past to apply a decorator to certain methods of all subclasses, without needing to specify that decorator in the subclass declaration itself.

1 comments

Came here to say this. This is my most common use of metaclasses, and __init_subclass__ looks to be a much cleaner solution.