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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Metaprogramming in Python

124 点作者 manjana超过 2 年前

14 条评论

samwillis超过 2 年前
This oft-repeated quote is always relevant on Python metaclass posts:<p>“[Metaclasses] are deeper magic than 99% of users should ever worry about. If you wonder whether you need them, you don’t (the people who actually need them know with certainty that they need them, and don’t need an explanation about why).”<p><i>Tim Peters, Inventor of the timsort algorithm and prolific Python contributor</i><p><a href="https:&#x2F;&#x2F;www.oreilly.com&#x2F;library&#x2F;view&#x2F;fluent-python&#x2F;9781491946237&#x2F;ch21.html" rel="nofollow">https:&#x2F;&#x2F;www.oreilly.com&#x2F;library&#x2F;view&#x2F;fluent-python&#x2F;978149194...</a><p><a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Tim_Peters_(software_engineer)" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Tim_Peters_(software_enginee...</a><p>I would then also concur with the other comment that if you “know” you need metaclasses, 99% of the time actually you only need __subclass_init__.<p>A lot of online literature about Python meta programming misses out __subclass_init__ as it was only added to Python 3.6 in 2015 via PEP 487.<p><a href="https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0487&#x2F;" rel="nofollow">https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0487&#x2F;</a>
评论 #32878856 未加载
评论 #32878184 未加载
narush超过 2 年前
This is a cool overview, and I certainly learned new things about the Python language from it. Thanks for posting!<p>We do lots of Python metaprogramming at Mito [1], but generally avoid all of this fancy Python fluff to get it done. Specifically, we avoid metaclasses, invisible decorators, etc. Instead, we take a much simpler approach of having Python code that literally populates as template .py file, and then writes it to the correct location in our codebase.<p>As a concrete example: we’re a spreadsheet, so we let our users transform data in a bunch of different ways - adding a column, writing a formula, deleting some rows. Anytime I want to add a new transform (say, encoding a column), I tell the metaprogramming package “python -m metaprogramming step —name “Encoding A Column”. It will ask me some questions about the parameters and types of those parameters, and then write most of the 4-6 boilerplate Python and Typescript files I need automatically! You can see it here [2].<p>This is still metaprogramming (it’s certainly code that writes code). But the code you end up with at the end of the day is very simple Python code that is extremely easy to understand &#x2F; maintain long-term.<p>I’ll pass on the fancy stuff for now. Thanks though!<p>[1] <a href="https:&#x2F;&#x2F;trymito.io" rel="nofollow">https:&#x2F;&#x2F;trymito.io</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;mito-ds&#x2F;monorepo&#x2F;blob&#x2F;dev&#x2F;mitosheet&#x2F;dev&#x2F;create_new_step.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mito-ds&#x2F;monorepo&#x2F;blob&#x2F;dev&#x2F;mitosheet&#x2F;dev&#x2F;c...</a>
评论 #32878538 未加载
评论 #32880972 未加载
kkirsche超过 2 年前
Thanks for sharing! Instead of using meta classes for subclass registration though, please just use __subclass_init__ instead. It’ll often be simpler unless you have a complex use case.<p><a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;reference&#x2F;datamodel.html#object.__init_subclass__" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;reference&#x2F;datamodel.html#object.__...</a>
评论 #32876512 未加载
wheelerof4te超过 2 年前
It is shocking how much vodoo magic there is in a programming language marketed as &quot;easy to learn and use&quot;.<p>And with the recent addition of new syntax, I would hardly call Python easy.
评论 #32877035 未加载
评论 #32877187 未加载
评论 #32878086 未加载
评论 #32877857 未加载
评论 #32878683 未加载
评论 #32877380 未加载
评论 #32878200 未加载
评论 #32878264 未加载
评论 #32885461 未加载
rroot超过 2 年前
Python is an amazing language. It&#x27;s basics are very easy to learn and I&#x27;d like to think it doesn&#x27;t feel like a incomprehensible sorcery to the beginner.<p>But even for that, it&#x27;s not a toy language as clearly evident by this example.
pdonis超过 2 年前
The article&#x27;s reference to &quot;classobj&quot; is wrong for Python 3 (that was the type of old-style classes in Python 2). In Python 3:<p><pre><code> &gt;&gt;&gt; class SomeClass: ... pass ... &gt;&gt;&gt; type(SomeClass) &lt;class &#x27;type&#x27;&gt;</code></pre>
评论 #32890938 未加载
ngcc_hk超过 2 年前
The reason why you have to reinvent lisp when you go deep enough is that if you are not, it is getting too hard and too confusing.
评论 #32878134 未加载
filipew超过 2 年前
Small typo at the start of the code examples.<p>We define the instance as: someobject = SomeClass()<p>but then we refer to it as <i>someobj</i> and <i>some_object</i>
评论 #32878285 未加载
carapace超过 2 年前
This is awesome! Now, never ever do it.<p>Python is powerful and flexible enough that you don&#x27;t need metaprogramming.<p>(I mean this quite literally: I sincerely doubt that there any code in Python using metaclasses etc., that wouldn&#x27;t be more clear and maintainable if rewritten in &quot;plain old&quot; Python without them.)<p>(With the caveat that I&#x27;m not including &quot;art&quot; projects, I&#x27;m talking about working production code.)<p>(In case it&#x27;s not clear, this is one of those &quot;Prove Me Wrong&quot; scenarios... If you think you have a counter-example to my claim, please don&#x27;t keep it to yourself, &quot;Shout it out so the whole theatre can hear you!&quot;)
评论 #32879296 未加载
评论 #32881088 未加载
评论 #32881021 未加载
ok123456超过 2 年前
It&#x27;s interesting how differently two similar dynlangs, Python and Ruby, decided to offer meta-programming. Ruby decided to offer a very simple way of evaluating code at construction, but python has you define custom metaclasses. One sees it as a routine way of extending the language, and the other treats it as a &#x27;black-art&#x27;.<p>Metaprogramming done wrong, no matter the mechanism, is very painful. So, I&#x27;m not sure which way is better.
behnamoh超过 2 年前
It’s interesting that meta programming, one of the main selling points of lisps, is actually pretty common and possible in other languages as well. Lisp’s “code = data” achieves meta programming in a straightforward way, but this link shows that it’s not necessary. In fact, Python’s way might even be better because the language sorta “gets outta the way” because it’s really darn simple.
评论 #32878388 未加载
评论 #32881942 未加载
xenophonf超过 2 年前
Stuff like this is how I created a very quick-and-dirty SaltStack module for AD Federation Services:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;irtnog&#x2F;salt-states&#x2F;blob&#x2F;production&#x2F;_modules&#x2F;identityserver_sts.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;irtnog&#x2F;salt-states&#x2F;blob&#x2F;production&#x2F;_modul...</a><p>The SaltStack module starts out empty except for a function that runs at module load time. The initialization function queries PowerShell for all AD FS-related cmdlets and creates Python wrapper functions for them. It even copies the cmdlet&#x27;s help to the function&#x27;s docstring.
stuckinhell超过 2 年前
As someone who just dabbles in python for small scripts, this is fascinating to me.<p>You can use some metaprogramming to create very clean interface points in python! I always wondered how django did so much with very clean readable implementations for end users.
siaw23超过 2 年前
very interesting, bookmarking for later. thanks.