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.

Specific ways to write better Python (2017)

154 pointsby guptarohitalmost 5 years ago

6 comments

dd82almost 5 years ago
The author has a second edition out with 90 examples at <a href="https:&#x2F;&#x2F;effectivepython.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;effectivepython.com&#x2F;</a>, and the corresponding github repo at <a href="https:&#x2F;&#x2F;github.com&#x2F;bslatkin&#x2F;effectivepython" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bslatkin&#x2F;effectivepython</a><p>Its been updated to be exclusive to 3.x, including 3.8 samples.
评论 #23589338 未加载
评论 #23586432 未加载
softwaredougalmost 5 years ago
On “return generators instead of lists” I think it’s also important when you do this to return use a context manager to manage the generators lifetime. Otherwise you might be surprised when the underlying file or source is closed later in your code.<p>I blogged about this: <a href="https:&#x2F;&#x2F;opensourceconnections.com&#x2F;blog&#x2F;2020&#x2F;06&#x2F;10&#x2F;python-generators-and-context-managers&#x2F;" rel="nofollow">https:&#x2F;&#x2F;opensourceconnections.com&#x2F;blog&#x2F;2020&#x2F;06&#x2F;10&#x2F;python-gen...</a>
评论 #23585468 未加载
评论 #23588623 未加载
评论 #23586010 未加载
cs702almost 5 years ago
Great suggestions for general-purpose code, but I would NOT recommend following them in &#x27;mathematically dense&#x27; code (e.g., deep learning models), for which being able to fit as much logic as possible into a single editing screen becomes increasingly important as we increase the complexity of the code.<p>Taken to the extreme, the &quot;fit as much logic as possible into a single screen&quot; approach leads naturally to code that looks a lot like something written in APL or one its descendants (J, K, etc.). I recognize this is not for everyone.<p>Personally, I think Jeremy Howard and the folks at fast.ai have struck a pretty good balance of readability and succinctness with their coding style for ML&#x2F;AI code:<p><a href="https:&#x2F;&#x2F;docs.fast.ai&#x2F;dev&#x2F;style.html" rel="nofollow">https:&#x2F;&#x2F;docs.fast.ai&#x2F;dev&#x2F;style.html</a>
评论 #23586926 未加载
评论 #23586417 未加载
carapacealmost 5 years ago
&gt; Assigning to a list slice will replace that range in the original sequence with what&#x27;s referenced even if their lengths are different.<p>Not only that, but you can assign to a slice with a stride:<p><pre><code> In [1]: r = list(range(10)) In [2]: r Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [3]: r[1::3] = 10, 20, 30 In [4]: r Out[4]: [0, 10, 2, 3, 20, 5, 6, 30, 8, 9]</code></pre>
nickysielickialmost 5 years ago
Items 7 and 8 seem to be at odds with each other to me, don&#x27;t use reduce and map because it&#x27;s ugly and unreadable, but if you use the alternative for reduce and map then you can&#x27;t make a deeper pipe because it becomes too complicated to read? Get real!<p>Is it even true that comprehensions are faster than reduce and map? I write a lot of python with reduce and map, and I know it&#x27;s not considered a best practice for perf reasons, but whenever I go-ahead and rewrite it in the &quot;more pythonic&quot; way, I can&#x27;t help but think that it ends up a lot less readable. I feel like this divide makes functional programming in python more of a hassle than it should be.
评论 #23588204 未加载
评论 #23590567 未加载
tumidpandoraalmost 5 years ago
I couldn&#x27;t help but notice that the latest commit on the repo was over 3 years ago