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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Python Language Features and Tricks

404 点作者 Bocker大约 11 年前

16 条评论

kriro大约 11 年前
Why are there so many negative comments? Maybe those posters are vastly underestimating how many people that just start out read HN. I think it&#x27;s a pretty good post to read after something like &quot;X in Y minutes - Python&quot; to get a very quick grasp of what the language is like.<p>I&#x27;m also not ashamed to say that despite having written quite a few LOC of Python I wasn&#x27;t aware of named slices for some reason and I think they can clear up some chunks of code I have produced (make it more readable)
评论 #7365746 未加载
评论 #7366733 未加载
评论 #7365641 未加载
评论 #7370749 未加载
评论 #7365675 未加载
densh大约 11 年前
Python&#x27;s unpacking is a poor man&#x27;s pattern matching. I&#x27;d really love to see them extend it to support user-defined patterns like Scala&#x27;s extractors or F#&#x27;s active patterns.
评论 #7365565 未加载
edwinnathaniel大约 11 年前
I&#x27;ve been using Python and Ruby on and off for a couple years (largely because I haven&#x27;t found the need to use it seriously day job or side projects).<p>One thing that strikes odd for me is how people describe Python&#x2F;Ruby are way more readable than Java.<p>I felt that Python, while more readable than Ruby (because Python uses less symbols), still contain more nifty tricks compare to Java.<p>It&#x27;s true that the resulting code is less code but behind that less line of code bugs might linger around because there might be plenty &quot;intents&quot; being hidden deep in the implementation of Python.<p>The Python way that is touted many times is &quot;explicit is better than implicit&quot; seems to correlate better with the much maligned &quot;Java is too verbose&quot;.<p>Anyhow, the other day I was refreshing my Python skill and learned the default implicit methods that I can override ( those eq, gte, gt, lte, lt) and I wonder how overriding those resulted in less lines of code compare to Java overriding equals, hashCode, and implementing one Comparator method than can return -1, 0, 1 to cover the whole spectrum of gte, gt, lte, (and even equality, given the context).<p>I suppose everything is relative...
评论 #7367163 未加载
评论 #7366611 未加载
评论 #7367254 未加载
JeffJenkins大约 11 年前
It&#x27;s important to remember that OrderedDict keeps <i>insertion</i> order, it isn&#x27;t an implementation of a sorted dictionary.
lqdc13大约 11 年前
zip to unzip a dict is a very slow approach to do it<p>Instead of<p><pre><code> mi = dict(zip(m.values(), m.keys())) </code></pre> Do<p><pre><code> mi = {v: k for (k, v) in m.iteritems()}</code></pre>
评论 #7365754 未加载
评论 #7365955 未加载
robinh大约 11 年前
I have two questions.<p>1. I&#x27;m unfamiliar with the term &#x27;unpacking&#x27;. Is it any different from pattern matching in, say, Haskell (but perhaps not as feature-rich)?<p>2. Aren&#x27;t slices pretty much a staple in Python? I didn&#x27;t think using them was considered a &#x27;trick&#x27;.
评论 #7365697 未加载
评论 #7365604 未加载
analog31大约 11 年前
Coming from a long history of languages like BASIC and Pascal, I will bookmark this tutorial. It seems to open up a lot of interesting Python features that were, quite frankly, not always easy to understand when described in plain text, but now seem pretty simple when presented as examples.<p>I&#x27;ll also think about the &quot;collection of simple examples&quot; next time I want to document something.
评论 #7367592 未加载
RK大约 11 年前
Nice reference.<p>1.29 happened to be exactly what I was looking for:<p><pre><code> for subset in itertools.chain(*(itertools.combinations(a, n) for n in range(len(a) + 1))) </code></pre> I spent <i>way</i> too much time writing a function to come up with these combinations.
评论 #7368421 未加载
yeukhon大约 11 年前
Slice has always been a painful adventure for me. I always forget that [1:3] is not all inclusive. It&#x27;s actually just range from 1 to 2.<p>I believe in 2.7 <i>zip</i> is still returning a list rather than an iterator (<i>izip</i> in Python 2, <i>zip</i> in Python 3+).<p>Another unappreciated stdlib is definitely <i>functools</i>. <i>mock</i> is also another awesome stdlib.<p><i>functools</i>, <i>collections</i> and <i>itertools</i> are definitely useful to make things faster. Also check out the list of stdlib. <a href="http://docs.python.org/2/library/" rel="nofollow">http:&#x2F;&#x2F;docs.python.org&#x2F;2&#x2F;library&#x2F;</a>
评论 #7367206 未加载
评论 #7368467 未加载
mamcx大约 11 年前
Great list, I do several mini-tutorials of python at <a href="http://runnable.com/u/mamcx" rel="nofollow">http:&#x2F;&#x2F;runnable.com&#x2F;u&#x2F;mamcx</a>. I try to pick several tricks for each theme
liyanage大约 11 年前
I think this is great, I&#x27;ve been doing Python for a while and I knew many of the features but I also learned a few new ones.<p>I don&#x27;t understand how this one to flatten lists works:<p><pre><code> a = [[1, 2], [3, 4], [5, 6]] [x for l in a for x in l] </code></pre> Can somebody explain what the order of operations is here and what the variables refer to in the various stages of evaluation?
评论 #7367212 未加载
评论 #7366363 未加载
evincarofautumn大约 11 年前
A good reference, to be sure, but man, do I resent the term “trick” in programming. It implies a deception, or something clever that you wouldn’t think to look for, like opening a wine bottle with a shoe. These aren’t tricks, they’re (largely) standard library features that you would simply <i>expect</i> to exist. But maybe I’m underestimating the NIH effect.
sebastianavina大约 11 年前
it&#x27;s amazing how much work and effort almost any of this examples would take to implement in C
评论 #7367404 未加载
overgard大约 11 年前
This is awesome, I&#x27;ve been programming python for about 8 years now and a lot of these still surprised me.
评论 #7367143 未加载
NAFV_P大约 11 年前
I know bugger all Python, but I know negative indexing.
评论 #7365796 未加载
jkork大约 11 年前
patterns &#x2F; tricks = language deficiencies<p>Wake me up when Python will support tail call elimination and will get rid of GIL. For now this language is no better than PHP.
评论 #7365564 未加载
评论 #7365762 未加载
评论 #7365800 未加载
评论 #7365613 未加载
评论 #7371503 未加载