5 ms·
> Understands and uses metaclasses In most circumstances, I feel like using metaclasses is a red flag. How many people are there on earth who understand and re
by 63 3y ago
> Understands and uses metaclasses
In most circumstances, I feel like using metaclasses is a red flag. How many people are there on earth who understand and regularly use meta classes when they're appropriate? I always pegged metaclasses as "you should know this exists in case you ever get unlucky enough to need it but otherwise stay the hell away from it"
- bgribble 3y agoThe one python pattern I used to use metaclasses for (registry types that keep an index of all their subclasses, very useful for DSL type tasks) is easier with __init_subclass__ and is somewhat more readable. So I haven't used metaclass in a minute. The worst thing about metaclass is that 95% of python programmers look at it and just go "whuzzat?" and require a seminar to catch a clue. It's actively hostile to whoever maintains the code after you.
- kstrauser 3y agoI'm with you completely about __init_subclass__. It covers almost all the uses where I'd otherwise use metaclasses. Junior engineers so that and say "oh, clever!", rather than running off screaming into the night.
- strbean 3y agoI too love attributes that can only be known at runtime and are therefore fundamentally un-type-hintable.
- nhumrich 3y agoAgree, but init_subclass is new to python 3. It almost feels like OPs advice is based on old python and not modern python.
- yen223 3y agoI have used metaclasses one time in my career, to simplify the way we generated a schema, in a framework where things are defined using classes. I agree with you in that it's nice to know that metaclasses exist, and that you can potentially reach for them in a pinch. I also think you can go really far in Python development without ever needing to know about metaclasses.
- wenc 3y ago"Metaclasses are deeper magic than 99% of users should ever worry about. If you wonder whether you need them, you don’t (the people who actually need them know with certainty that they need them, and don’t need an explanation about why)." — Tim Peters I once used metaclasses because I was writing a code generator for a DSL. But that was the only time. Conceptually they're not too difficult but more likely than not YAGNI. https://realpython.com/python-metaclasses/ https://realpython.com/python-metaclasses/