4 ms·
Am I just bad at OOP hackery or does anyone else look at this and think it'd be extremely hard to debug?
by lvass 5y ago
Am I just bad at OOP hackery or does anyone else look at this and think it'd be extremely hard to debug?
- foolfoolz 5y agoi wouldn’t let this past code review. it’s magic and makes a great blog post about some feature but is not maintainable code
- pmarreck 5y agoYep. As a current functional-language guy, this merely confirms my suspicion that those crazy functional people were onto something. As it turns out, since the human mind is the real limitation here, anything that stops runaway complexity ("hiding" functionality behind "magic" is actually worse because you're just moving the complexity "away from your mental model" where it can fester and grow out of sight, even if what you see looks "simpler") results in fewer bugs, and more-debuggable bugs.
- qsort 5y agoFrankly I doubt you even need metaclasses in the first place (at least in non-library code). I get that it's a dynamic language but if I need metaclasses for something it's more likely than not to be a hidden technical debt generator.
- vosper 5y agoI think SQLAlchemy makes good use of metaclasses. It's the only thing I've seen that used them in a way that I thought made sense and was well justified (but I have been out of the Python world for a while). In another language it would be done with code-gen, which would also be fine. But SQLAlchemy is library code. I took the time to understand how to use metaclasses, and concluded that I never would, especially not in code I expected another engineer to understand - I expect 90% of people who write Python haven't heard of metaclasses, and 99% have never written one.
- nomel 5y agoThere was a time, early on in my python career, that I tried all the different flavors of magic that python provides. These days, I tend to write code for junior developers. I don’t use any magic at all, even when it could make sense. Once you start playing with magic, you have far fewer eyes and hands that are interested.
- nightski 5y agoIn my opinion it's not about dealing with junior developers or even having the ability to understand it. I've been programming for 25 years and to me it's about cognitive load. I despise tricks like this because they put a much higher strain on the programmer. Software is hard enough, let's not make it harder. If I can look at a piece of code and it's quickly obvious whether it is correct or not is my favorite type of code...
- draw_down 5y ago
- jreese 5y agoI'm not fond of the example given in the post, but I've very happily used __init_subclass__ in a few places where I previously would have needed a decorator. Eg, for "plugin-like" things where I would have otherwise needed a decorator to keep track, or iterated over subclasses, now can just "register" subclasses without any extra work on the child classes. Things that I normally wouldn't expect to change the behavior of the subclass, but where having a no-effort hook at subclass creation time can be extremely useful.
- sillysaurusx 5y agoI used to think so, but cls.__subclasses__() makes it possible to just walk the type tree at some later point. I find the lack of state to be a feature. (Normally with a hook like you're talking about, you'd add the class to some list somewhere, which can get out of sync.)
- formerly_proven 5y agoMetaclasses / __init_subclass__ are generally only used if you try to define different / new behavior for how the body of a class is interpreted. So if you'd want to create e.g. an explicit interface system in Python, you might use them. If you're creating an ORM or some other kind of declarative property system, you might use them (but not necessarily, as descriptors are a thing). I think the only use in the standard library is in ABCs and probably the typing module. Edit: Enums use them as well (makes sense).
- klodolph 5y agoIt's used very sparingly in the wild, for cases where you need a bunch of classes that are set up similarly, and ordinary subclassing still results in too much boilerplate. For example... you might make a class for each HTML tag in an HTML processor, or each drawing command in an SVG processor. My experience is that it's not hard to debug, because all that you're doing is getting rid of some boilerplate.
- Traubenfuchs 5y agoI am baffled that people make fun of Java and Spring and then use double underscore magic methods called __init_subclass__ with a straight face.
- RhysU 5y agoI once spent a week trying to use this kruft to make a custom class hierarchy accept dataclass annotations. I was successful but damn.