TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Python Language Features and Tricks

404 pointsby Bockerabout 11 years ago

16 comments

kriroabout 11 years ago
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 未加载
denshabout 11 years ago
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 未加载
edwinnathanielabout 11 years ago
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 未加载
JeffJenkinsabout 11 years ago
It&#x27;s important to remember that OrderedDict keeps <i>insertion</i> order, it isn&#x27;t an implementation of a sorted dictionary.
lqdc13about 11 years ago
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 未加载
robinhabout 11 years ago
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 未加载
analog31about 11 years ago
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 未加载
RKabout 11 years ago
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 未加载
yeukhonabout 11 years ago
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 未加载
mamcxabout 11 years ago
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
liyanageabout 11 years ago
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 未加载
evincarofautumnabout 11 years ago
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.
sebastianavinaabout 11 years ago
it&#x27;s amazing how much work and effort almost any of this examples would take to implement in C
评论 #7367404 未加载
overgardabout 11 years ago
This is awesome, I&#x27;ve been programming python for about 8 years now and a lot of these still surprised me.
评论 #7367143 未加载
NAFV_Pabout 11 years ago
I know bugger all Python, but I know negative indexing.
评论 #7365796 未加载
jkorkabout 11 years ago
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 未加载