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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

WTF Python: Exploring and understanding Python through surprising snippets

359 点作者 srathi超过 4 年前

19 条评论

Doxin超过 4 年前
I feel like WTFs caused by doing something <i>deeply weird</i> hardly counts against the language.<p>Yes doing the walrus operator inside brackets works. Yes of course it&#x27;s going to be doing weird things. Is this really surprising to anyone? It&#x27;s not what that operator is <i>for</i>.<p>Yes comparing strings with &quot;is&quot; works <i>sometimes</i>. It&#x27;s not checking equality and it isn&#x27;t supposed to. In another implementation using &quot;is&quot; might work all the time or none of the time. Don&#x27;t use &quot;is&quot; to compare strings. it&#x27;s not what that&#x27;s for.<p>Yes chaining mismatching operations does weird things. Be clearer in how you write your code. It&#x27;s no surprise that writing confusing code results in confusing results.<p>I&#x27;ve been seeing a growing anti-python sentiment lately, and most of it seems to be based on these sorts of odd perceived language defects. Python is by no means perfect, but none of the code on this page is something anyone would actually write in production code on purpose.<p>I suppose it&#x27;s a sign of python getting more popular.
评论 #26100318 未加载
评论 #26099521 未加载
评论 #26099277 未加载
评论 #26100870 未加载
评论 #26099313 未加载
评论 #26099302 未加载
评论 #26099853 未加载
评论 #26101496 未加载
评论 #26099321 未加载
评论 #26099486 未加载
评论 #26104938 未加载
评论 #26106762 未加载
评论 #26099328 未加载
评论 #26106082 未加载
评论 #26099509 未加载
评论 #26101360 未加载
tgbugs超过 4 年前
While this repo is great for education, it doesn&#x27;t cover the deep problems with Python that you only discover when you try to do something &quot;unapproved&quot; or slightly off the happy path. Depending on the domain you are working in you may never encounter such a situation, but if you do, be prepared to lose your sanity one omnipresent design flaw at a time.<p>Over the years I&#x27;ve been collecting a series of cases that are somewhat more nihilistic and depressing under the general category of LOL PYTHON. The little inconsistencies, the boilerplate that is required if you don&#x27;t want to use only the approved, not performant, and breaks all sorts of other hidden assumptions so you can&#x27;t actually use the code outside very narrow settings approach, eventually just evoke a nihilistic chuckle and a note to never start a new Python project again.
评论 #26099475 未加载
评论 #26099437 未加载
评论 #26099473 未加载
bdg超过 4 年前
To be frank I think the python community lacks self-awareness.<p>There&#x27;s this ritual you have to do when learning python: blindly accept status-quo design choices as &quot;universally good&quot; (but call it &quot;pythonic&quot;), and crap on other languages. Never admit python or the ecosystem has issues or shortcomings, and if another language has something better than yours (eg, Yarn, or Composer is way better than anything in Python (poetry is a good step but not a competitor)), or offer really good IoC tools: just double-down on the status-quo and say something like &quot;looks over engineered, I see no reason why I would need that&quot;.<p>Pythonic is putting a decorator everywhere, coupling the system together into a ball of mud where you can&#x27;t decouple it. Pythonic is having annoying mixed naming conventions. Why do I need methods to have underscores in 2021, but classes to use camel case? Why do my lines of code need to fit on a punched card invented decades before the language was invented? Why does the idiomatic &quot;one true way&quot; need to be what some other engineer thinks is &quot;the one true&quot; way?<p>Python is a fractal of closed-mindedness and single-use code. There&#x27;s a reason it took a decade to get projects onto python 3, but PHP can swiftly move the entire ecosystem from v5 to v7 in a year. FastAPI is getting a lot of praise recently, but frameworks of this style are a dime-a-dozen in other languages and have been around for a decade.<p>Pythonic is a meaningless word that just gives firepower to the most confident-sounding engineer in a company to shoot down anyone who doesn&#x27;t code like them. It&#x27;s not an objective concept, it&#x27;s innovation poison.
评论 #26100141 未加载
评论 #26100012 未加载
评论 #26100183 未加载
评论 #26099694 未加载
评论 #26099824 未加载
评论 #26099797 未加载
评论 #26101559 未加载
评论 #26099683 未加载
评论 #26101449 未加载
alpaca128超过 4 年前
Ah yes, the one about mutable default arguments was a real surprise for me once in a debugging session. Python only initialising them once during the whole script runtime is not exactly the behaviour anyone would expect.
评论 #26099183 未加载
评论 #26099494 未加载
j0057超过 4 年前
Python should just remove the `is` operator. If you really need to compare object identities, which is already a rare scenario to begin with, you can explicitly compare the object IDs. Checking if something is or is not True&#x2F;False&#x2F;None can just be treating the thing as True-ish or False-ish. Making the language bigger is not nearly as impressive as making it smaller!
评论 #26099474 未加载
评论 #26099555 未加载
评论 #26099603 未加载
gspr超过 4 年前
The Python&#x2F;NumPy gotcha that has caused me the most pain by far is<p><pre><code> def foo(x, y): return x+y </code></pre> where the <i>intention</i> is for x and y to be NumPy arrays, but the caller accidentally passes lists (or vice versa). This is especially common because a lot of NumPy functions are agnostic as to whether they operate on arrays or other iterables.
评论 #26100485 未加载
评论 #26122510 未加载
评论 #26101748 未加载
fireattack超过 4 年前
Compared to wtfjs, I&#x27;d say this is pretty tame.<p>The only one I actually encountered myself is `def some_func(default_arg=[]):` one, but it was indeed a very &quot;WTF&quot; moment.
评论 #26100413 未加载
Groxx超过 4 年前
This is a really impressive list of oddities, with solid explanations on every single one. Excellent resource, thanks!
pansa2超过 4 年前
<p><pre><code> &gt;&gt;&gt; another_tuple ([1, 2], [3, 4], [5, 6, 1000]) &gt;&gt;&gt; another_tuple[2] += [99, 999] TypeError: &#x27;tuple&#x27; object does not support item assignment &gt;&gt;&gt; another_tuple ([1, 2], [3, 4], [5, 6, 1000, 99, 999]) </code></pre> This one&#x27;s my favorite. Although it&#x27;s a rare issue that you&#x27;re unlikely to come across, it&#x27;s a side-effect of a much bigger design flaw in Python - the decision to make `a += b` behave differently from `a = a + b`.
评论 #26099376 未加载
评论 #26099322 未加载
Maakuth超过 4 年前
Ah, brings to my mind the classic talk: <a href="https:&#x2F;&#x2F;www.destroyallsoftware.com&#x2F;talks&#x2F;wat" rel="nofollow">https:&#x2F;&#x2F;www.destroyallsoftware.com&#x2F;talks&#x2F;wat</a>
评论 #26099046 未加载
评论 #26098863 未加载
coldtea超过 4 年前
<p><pre><code> # Python version 3.8+ &gt;&gt;&gt; a = 6, 9 &gt;&gt;&gt; a (6, 9) &gt;&gt;&gt; (a := 6, 9) (6, 9) &gt;&gt;&gt; a 6 &gt;&gt;&gt; a, b = 6, 9 # Typical unpacking &gt;&gt;&gt; a, b (6, 9) &gt;&gt;&gt; (a, b = 16, 19) # Oops File &quot;&lt;stdin&gt;&quot;, line 1 (a, b = 6, 9) ^ SyntaxError: invalid syntax &gt;&gt;&gt; (a, b := 16, 19) # This prints out a weird 3-tuple (6, 16, 19) &gt;&gt;&gt; a # a is still unchanged? 6 &gt;&gt;&gt; b 16 </code></pre> In what way is ANY of this unexpected?<p>For starters, unpacking is not the same as assigning <i>inside</i> a tuple...<p>(And how is the 3-tuple &quot;weird&quot;? It prints exactly what you did...)
a-dub超过 4 年前
all these cool new features! what&#x27;s the thing they always say about python? &quot;there&#x27;s more than one way to do it&quot;?
评论 #26099214 未加载
评论 #26099324 未加载
pachico超过 4 年前
If this article was about the WTFs in PHP there would already be hundreds of comments saying how much it sucks.<p>You get less criticism about other languages although they also have bad design decisions (which is totally fine since nothing is perfect).<p>I wish these things weren&#x27;t so biased by the emotional investment people put in working tools, which is all a language is.
trfarmer超过 4 年前
My biggest issue with Python that is not related to syntax is that it isolates you from the Unix machine in a bad way.<p>I get that they try to support Windows with the same abstractions, but they are all leaky and subtly broken (like shutil). If you use Python too much, you lose the understanding of Unix.<p>Other languages aren&#x27;t like that, for example Perl and Go.<p>Whenever I stop using Python for 2 weeks and focus on C or Go, my understanding of the machine and what is actually going on increases dramatically. I&#x27;ve even considered learning Perl, but haven&#x27;t done it yet due to lack of time.
评论 #26100847 未加载
CivBase超过 4 年前
<p><pre><code> &gt;&gt;&gt; a := &quot;wtf_walrus&quot; File &quot;&lt;stdin&gt;&quot;, line 1 a := &quot;wtf_walrus&quot; ^ SyntaxError: invalid syntax </code></pre> Why <i>doesn&#x27;t</i> this work? Isn&#x27;t the walrus operator effectively just an alternative to the assignment operator which also returns the assigned value? Why specifically make it fail in this situation?
评论 #26101648 未加载
matsemann超过 4 年前
Tips on learning python as a seasoned dev?<p>I will start a new job where everything is done in Python in a few weeks. Learning the syntax of the language is easy, but what about &quot;everything else&quot; using a language entails? Like how is stuff written in practice (pythonic &#x2F; idiomatic), build tools, libs everyone use, workflow, etc
评论 #26100993 未加载
rurban超过 4 年前
Looks like in C++, where the maintainers don&#x27;t dare to say no to add simple syntax to win popularity contests, but hereby completely destroy the language. JavaScript also comes to my mind.<p>walrus, haha
memming超过 4 年前
TIH python
davesque超过 4 年前
Oh look. Another techie trying to gain cred by hating on things.