Hacker News new | ask | show | jobs
by carapace 1370 days 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!")

2 comments

The 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.
Too 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.

What is an example of a good orm alternative? Don’t remember sqlalchemy being substantially different on the model definition front.
Pydantic uses metaprogramming to generate runtime validations according to the field type annotation.