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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

A Guide to Python's Magic Methods

234 点作者 cskau大约 13 年前

6 条评论

mitchellh大约 13 年前
This is a great post, but I also want to point out that this page is invaluable to any Python developer wanting to know the details about the magic methods: <a href="http://docs.python.org/reference/datamodel.html#special-method-names" rel="nofollow">http://docs.python.org/reference/datamodel.html#special-meth...</a><p>Whenever working on any reasonably complex class, I find that open in a tab to verify that I'm using the magic methods properly, and that they'll be called as I expect.
评论 #3717707 未加载
steve-howard大约 13 年前
&#62;__getattr__(self, name)<p>&#62; ... This can be useful for catching and redirecting common misspellings ...<p>I can think of only one <i>good</i> reason to redirect spelling errors, and that's if you are a widely-used library and you want to support both "color" and "colour" (and similar Britishisms). Other than that, you're just asking for a headache.
评论 #3717815 未加载
评论 #3718742 未加载
MikeOnFire大约 13 年前
For each special method, you should show what syntax magically invokes it. Something like this:<p>foo.__iter__() is invoked by for x in foo a.__iadd__(b) is invoked by a += b a.__rmult__(b) is invoked by b * a when b * a fails. foo.__len__() is invoked by if foo when foo.__bool__ or foo.__nonzero__ is not defined. foo.__repr__() is invoked by '%r' % foo, also by pprint.pprint(foo) and deprecated `foo`. It's also invoked by __str__ magic when foo.__str__ is not defined. etc. Putting the above info in a table would also be helpful.
评论 #3717302 未加载
评论 #3717416 未加载
zanny大约 13 年前
This might be tangental, but the syntax of having magic methods be surrounded by double underscores always drives me insane. For a language designed around readability, it seems like the python dev team just kicked the can on things like __init_ and said "Sure, we have a few dozen english words already restricted usage in the language, but let's kick the whole "pretty" thing out and start wrapping every special statement in ugly underscores like its 1995 again!".<p>They probably started this trend around 1995, now that I think about it, ya know. since the language is that old and all.<p>It is still really annoying, I just cringe when I use __init__ constructors because they look so ugly in otherwise very English-document-like source.<p>&#60;/rant&#62;
评论 #3719810 未加载
评论 #3719168 未加载
评论 #3720994 未加载
njharman大约 13 年前
Controlling Attribute Access section is really poor. Giving poor explanation of getattr vs getattribute, wrongly recommending to not use these methods, claiming there is little use, and providing poor examples of common use cases. The biggest of which is object composition.
评论 #3719643 未加载
sophacles大约 13 年前
One of the better treatments of __get__ and __set__ I've seen. Descriptors are deep and powerful magic, and a bit complex, but when they are appropriate, they are less complex than the alternatives, and allow for cleaner interfaces.