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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Python Best Practice Patterns

311 点作者 abeinstein超过 11 年前

16 条评论

bru超过 11 年前
Several of those patterns are incomplete or frowned upon:<p>* if a method does not use the object&#x27;s state (no `self` usage) make it a `class-` or `staticmethod`.<p>* Some magic methods are presented. There&#x27;s more to them[0].<p>* one should not write `class MyClass:` but `class MyClass(object):` (new style class[1])<p>* the last one (`return None`) make me very dubious<p>* Cascading methods: that&#x27;s a big <i>no</i>. The idiom is that if a method may change the state of the object then it should return None (eg `set.add`)<p>0: well-written and comprehensive guide: <a href="http://www.rafekettler.com/magicmethods.html" rel="nofollow">http:&#x2F;&#x2F;www.rafekettler.com&#x2F;magicmethods.html</a><p>1: <a href="http://www.python.org/doc/newstyle/" rel="nofollow">http:&#x2F;&#x2F;www.python.org&#x2F;doc&#x2F;newstyle&#x2F;</a>
评论 #7320161 未加载
评论 #7320708 未加载
评论 #7324351 未加载
评论 #7320344 未加载
评论 #7320140 未加载
评论 #7320177 未加载
评论 #7321230 未加载
评论 #7322676 未加载
评论 #7320410 未加载
评论 #7321061 未加载
SoftwareMaven超过 11 年前
Having gone through a couple thousand lines of Javascript that adhered to the &quot;keep methods small&quot;, I call bollocks on that. Make methods as big as they need to be.<p><pre><code> function doFooOnList(l) { for (var i=0; i&lt;l.length; i++) { doFoo(l[i]); } } function doFoo(i) { i.foo(); } </code></pre> gets old, very quickly.<p>After designing and building code for 20+ years, I can comfortably say that there are no arbitrary rules of software design, and some of the worst code I&#x27;ve seen has been a result of following &quot;best practices&quot; instead of thinking for oneself.<p>Write code like it is meant to be read, because that&#x27;s what happens most often.
评论 #7321743 未加载
评论 #7323085 未加载
评论 #7321264 未加载
评论 #7323566 未加载
评论 #7321262 未加载
IgorPartola超过 11 年前
Agree with all of these but two. The first is the example of doing:<p><pre><code> class Foo(object): highlight = reverse </code></pre> No, that is not clearer. Now I have no idea what this method does. Making it explicit requires more keystrokes, but allows you to properly document the method. Also, when I run help(Foo.highlight) I won&#x27;t get the generic documentation for `reverse`.<p>Second, using `each` for a generic iteration variable. This is an opinion, not a best practice. I would argue that either the loop is a one liner, at which point use whatever you want (x works well), or it&#x27;s more than one line and then I want a proper name for the thing you are iterating over.
评论 #7321062 未加载
评论 #7320878 未加载
sloria超过 11 年前
Author here. Must give credit where it&#x27;s due:<p>These patterns come from a talk by Vladimir Keleshev, author of docopt and excellent Pythonista. These are NOT my original work.
评论 #7320233 未加载
评论 #7322821 未加载
atrk超过 11 年前
Several of these are generally applicable to programming:<p>* Keep functions small and composable<p>* Keep functions at a consistent level of abstraction<p>* Use constructors to ensure objects always exist in a complete, usable state<p>* Use meaningful method names in place of comments<p>It is nice to see that other people struggle with functions with lots of parameters + lots of partial state variables. I don&#x27;t suppose anyone here has a better solution?
评论 #7320290 未加载
评论 #7320309 未加载
unoti超过 11 年前
I never knew you could use __enter__ and __exit__ to code your own things that work with the &#x27;with&#x27; statement. Well worth the read!
评论 #7319950 未加载
评论 #7319969 未加载
评论 #7320097 未加载
abecedarius超过 11 年前
For the &#x27;method object&#x27; one, I use nested functions. Python is not Java or Smalltalk.<p>The @classmethod example I&#x27;d write with an ordinary function also, outside the class.
frodopwns超过 11 年前
Not enough explanation as to what problems are being solved or why your solutions are &quot;best practices&quot;.
simon_weber超过 11 年前
Many of these tips address the idea that good naming improves readability. I couldn&#x27;t agree more!<p>If you&#x27;re looking for more on this topic, Brandon Rhodes gave an excellent talk on this at PyCon US last year [0].<p>[0] <a href="http://pyvideo.org/video/1676/the-naming-of-ducks-where-dynamic-types-meet-sma" rel="nofollow">http:&#x2F;&#x2F;pyvideo.org&#x2F;video&#x2F;1676&#x2F;the-naming-of-ducks-where-dyna...</a>.
CmonDev超过 11 年前
And the most important pattern:<p><a href="http://stackoverflow.com/questions/1275646/python-3-and-static-typing" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;1275646&#x2F;python-3-and-stat...</a>
评论 #7320400 未加载
codelucas超过 11 年前
This is the guy who authored TextBlob! <a href="https://github.com/sloria/TextBlob" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sloria&#x2F;TextBlob</a>
lukasm超过 11 年前
I&#x27;m skeptical about the last one return None
评论 #7320142 未加载
评论 #7320144 未加载
Axsuul超过 11 年前
Is there something like this for Ruby?
Jiliwang超过 11 年前
Very learnsome, Thanks!
binarysolo超过 11 年前
Commenting to save for later reading. :)
评论 #7321156 未加载
Walkman超过 11 年前
You should name a classmethod first variable &quot;cls&quot;, not &quot;class_&quot;.