TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Understanding Python metaclasses

2 点作者 ionelm超过 10 年前

2 条评论

carapace超过 10 年前
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.
ionelm超过 10 年前
It would be very interesting to hear how the object system looks (comparatively) in other languages.