TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Understanding Python metaclasses

2 pointsby ionelmover 10 years ago

2 comments

carapaceover 10 years ago
Metaclasses are awesome.<p>But the moment you put one in your codebase you have a ticking time bomb just waiting to blow up in the face of some unsuspecting programmer who doesn&#x27;t know how they work. I&#x27;ve seen this in production with Django forms. Someone had to extend a form class and couldn&#x27;t do it and couldn&#x27;t figure out why, they had to ask me for help. (We wrote a little factory function to make the form instance first and patch it after but that&#x27;s damn rude to have to do.)<p>The point is this: there is a <i>HUGE</i> conceptual overhead to using metaclasses, huge. People like me will read GVR&#x27;s &quot;Killing Joke&quot; article for kicks, because we <i>like</i> the feeling of our brains catching fire and exploding. But junior devs&#x27; brains should NOT be exploding at work! (For goodness&#x27; sake, he named it after the Python skit with the joke so funny it kills you from laughter, the English use it on the Germans in WWII. He named it that for a reason!)<p>If you want to use metaclasses do it in your own home and wash your hands afterwards.<p>Seriously though, please <i>only</i> use metaclasses in your production code (anything you want other people to use, and that isn&#x27;t some experiment) if you absolutely cannot think of a simpler, more conventional way to accomplish what you&#x27;re trying to do.<p>And, really, Python is so powerful and expressive I don&#x27;t think you will find yourself stumped very often.
ionelmover 10 years ago
It would be very interesting to hear how the object system looks (comparatively) in other languages.