4 ms·
This is awesome! Now, never ever do it. Python is powerful and flexible enough that you don't need metaprogramming. (I mean this quite literally: I sincerely
by carapace 4y ago
This is awesome! Now, never ever do it.
Python is powerful and flexible enough that you don't need metaprogramming.
(I mean this quite literally: I sincerely doubt that there any code in Python using metaclasses etc., that wouldn't be more clear and maintainable if rewritten in "plain old" Python without them.)
(With the caveat that I'm not including "art" projects, I'm talking about working production code.)
(In case it's not clear, this is one of those "Prove Me Wrong" scenarios... If you think you have a counter-example to my claim, please don't keep it to yourself, "Shout it out so the whole theatre can hear you!")
- stuckinhell 4y agoThe example shows that django models uses metaclasses heavily. So I'm assuming if you needed to extend django models, you'd have to do it via metaclasses.
- carapace 4y agoToo easy: Django meta-programming is one of the things that has bitten IRL. A junior dev was pulling hair out one day trying to subclass a Django HTML form object. Couldn't do it because of Django cowboy metaclass. Had to wrap it in a "factory" and modify it before returning it. Django using metaclasses is exactly the problem I'm warning of... You don't need metaclasses for data models nor for HTML forms. Nothing Django does requires metaclasses, and it's actively beginner-unfriendly to use them.
- mixmastamyk 4y agoWhat is an example of a good orm alternative? Don’t remember sqlalchemy being substantially different on the model definition front.
- deleted 4y ago[deleted]
- pdhborges 4y agoPydantic uses metaprogramming to generate runtime validations according to the field type annotation.