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.

WTFPython: Exploring and understanding Python through surprising snippets

460 pointsby giladalmost 3 years ago

17 comments

praptakalmost 3 years ago
I have a mildly surprising snippet which I believe they don&#x27;t have. It&#x27;s about interaction of class and instance attributes with behavior of &quot;X += Y&quot;. Python expands it to &quot;X = X + Y&quot; but only if X implements &quot;+&quot; but not &quot;+=&quot;<p><pre><code> class A: a = (1,) # tuples are immutable and don&#x27;t have &quot;+=&quot; b = [1,] # lists have &quot;+=&quot; obj = A() obj.a += (2,) # this creates obj.a obj.b += (2,) # this modifies A.b print(A.a, A.b) </code></pre> The snippet prints &quot;(1,) [1,2]&quot;.
评论 #31567035 未加载
评论 #31568350 未加载
评论 #31568166 未加载
评论 #31567286 未加载
评论 #31573148 未加载
评论 #31568109 未加载
评论 #31567872 未加载
评论 #31572302 未加载
satwikalmost 3 years ago
Hi, maintainer of the repo here, thanks for sharing :)<p>If anyone&#x27;s willing to go through those examples with an interpreter on the side, you can check out <a href="https:&#x2F;&#x2F;www.wtfpython.xyz&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.wtfpython.xyz&#x2F;</a><p>It&#x27;s built with pyiodide, the only limitation is you may not get correct results for the examples that are version-dependent. The UX might not be great, especially on mobile (happy to hear ideas on how to improve it), but it does the job for now :)
评论 #31566898 未加载
评论 #31567676 未加载
评论 #31572304 未加载
systemvoltagealmost 3 years ago
Walrus operator IMO is a huge mistake. It saves fuck all time&#x2F;space, but introduces an entire new operator that probably added more complexity, edge cases and quirks than the benefits it provides. <i>Terrible</i> idea.<p>Worse, no one uses it. I&#x27;ve yet to come across anyone that advocates it or remembers it.
评论 #31566435 未加载
评论 #31566414 未加载
评论 #31566739 未加载
评论 #31566765 未加载
评论 #31566471 未加载
评论 #31566426 未加载
ezoealmost 3 years ago
I have to write Python for the job right now and this language has pitfalls everywhere. Glad I avoided this language so far.<p>I don&#x27;t want to use Python for a software more than a few dozen lines or involving multiple developers. This language was designed to unintentionally shoot your foot so many times.
评论 #31566992 未加载
评论 #31566938 未加载
评论 #31572377 未加载
评论 #31574944 未加载
评论 #31575047 未加载
cmseftonalmost 3 years ago
Previous discussions: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21862073" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21862073</a> (2019) <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=26097732" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=26097732</a> (2021)
makachalmost 3 years ago
I picked up Python again a couple of months ago, and I am reexperiencing my love&#x2F;hate relationship with the language.<p>Reading through the documentation in WTFPython I get some kind of affirmation of my bias against python. I hate it, but I also love it, but I hate it. It is just so &quot;je ne sais quoi&quot;--<p>The docs would benefit from some explanations on whys.
评论 #31567697 未加载
moffkalastalmost 3 years ago
&gt; The Walrus operator (:=) was introduced in Python 3.8, it can be useful in situations where you&#x27;d want to assign values to variables within an expression.<p>Ok that is infuriating. They dragged their feet through the dirt on how the switch statement is useless and pointless, but then they add some random bullshit like that that nobody ever asked for.
评论 #31572447 未加载
NmAmDaalmost 3 years ago
I learned a couple of things there, seems like a very organized way of learning.Just wish more of this approach is implemented by others.<p>Also the discussions in comments makes me wonder if HN should implement syntax highlighting feature.
Aachenalmost 3 years ago
There are some genuinely surprising ones, but is it just me who seems to think these are for 80% either well-known beginner&#x27;s mistakes (== vs. is, underscored names) or just begging for problems with operator precedence (by omitting parentheses) and variable scoping (by shadowing variables)?<p>Random example I&#x27;m currently reading is: x, y = (0, 1) if True else None, None. The interpreter considers the last None outside the if statement. Gee, so you thought to need parentheses at first but not for the second one, and now it&#x27;s a pitfall that parentheses are missing? The readme goes so far as so say &quot;I haven&#x27;t met even a single experience Pythonist till date who has not come across one or more of the following scenarios&quot;. Uh huh. Not me.<p>Add parentheses when in doubt, and when not in doubt, add parentheses for readability. In any language. Not excessively, like everyone knows what &quot;if x == 2:&quot; does without them in python, but for anything less obvious or more complicated than a=2*2+3, just do it.
Sohcahtoa82almost 3 years ago
I&#x27;ve seen these before and every single time, it&#x27;s mostly based on a misunderstanding of certain language features that only a beginner coder would do (for example, not knowing that Python does not do block-level variable scoping, so loop variables leak) or an abuse of an implementation detail (for example, integers under 255 being singletons).<p>&quot;is&quot; is not the same as &quot;==&quot;. Confusing the two is a rookie mistake, yet is treated like a &quot;gotcha&quot;.
评论 #31571000 未加载
评论 #31571725 未加载
behnamohalmost 3 years ago
In Python, I&#x27;ve always run into the problem of expression-vs-statement. For example, in my mind I want to do this:<p><pre><code> (a++ if a==b else b++) + 1 </code></pre> Or something like this (borrowed from <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;65024477&#x2F;walrus-operator-in-list-comprehensions-python" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;65024477&#x2F;walrus-operator...</a>):<p><pre><code> from datetime import datetime timestamps = [&#x27;30:02:17:36&#x27;, &#x27;26:07:44:25&#x27;, &#x27;25:19:30:38&#x27;,&#x27;25:07:40:47&#x27;] timestamps_dt = [ datetime(days=day,hours=hour,minutes=mins,seconds=sec) for i in timestamps day,hour,mins,sec := i.split(&#x27;:&#x27;)] </code></pre> But this type of stuff is always greeted with Python&#x27;s inflexible syntax. Recently, I learnt about Rackets&#x27;s idea that &quot;everything is expression&quot; on (<a href="https:&#x2F;&#x2F;beautifulracket.com&#x2F;appendix&#x2F;why-racket-why-lisp.html#:~:text=Beautiful%20Racket%20and%20Practical%20Typography,is%20a%20descendant%20of%20Lisp" rel="nofollow">https:&#x2F;&#x2F;beautifulracket.com&#x2F;appendix&#x2F;why-racket-why-lisp.htm...</a>.) and was blown away to realize these are valid Racket codes:<p><pre><code> ((if (&lt; 1 0) + *) 42 100) </code></pre> or<p><pre><code> (+ 42 (if (&lt; 1 0) 100 200)) </code></pre> The second one has an equivalent in Python:<p><pre><code> 42 + (100 if 1 &lt; 0 else 200) </code></pre> But the first one does not:<p><pre><code> 42 (+ if 1 &lt; 0 else *) 100 </code></pre> &gt; SYNTAX ERROR.<p>Maybe it&#x27;s time for me to get my hands dirty with Lisp.
评论 #31567377 未加载
评论 #31566420 未加载
评论 #31567253 未加载
评论 #31566333 未加载
评论 #31566326 未加载
beckingzalmost 3 years ago
This is a fun and well written set of examples, which shows and explains the cause of the counter intuitive behavior.
cuteboy19almost 3 years ago
The wtfs about id and is are not deserved. These operations are not used in normal code and are explicitly implementation defined. I suppose they should have named it __is and __id. I don&#x27;t see why anyone not hacking the interpreter would care about these though
评论 #31567711 未加载
psychoslavealmost 3 years ago
Interesting. I would be interested with a WTFRuby. As I lately explored Ruby deeper, I was surprised that Complex would remove #% as well as most Comparable operators.<p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;72314247&#x2F;how-can-ruby-complex-class-have-comparable-in-its-ancestors-when-it-has-none" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;72314247&#x2F;how-can-ruby-co...</a><p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;71439853&#x2F;why-ruby-doesn-t-provide-a-complexemodulo-method" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;71439853&#x2F;why-ruby-doesn-...</a>
gnramiresalmost 3 years ago
I&#x27;ve been thinking it would be useful to create a sub-language of python with only essential features (removing hard to explain features such as &#x27;is&#x27;, &#x27;:=&#x27;, and a plethora of non-essentials) -- that is, fully backward-compatible with python. This would also make restricted interpreters easier to write as well. I know micropython exists, but the goal is more minimal size than simplicity and ease to learn.
bjarnehalmost 3 years ago
That Python uses the exact same string if they contain the same immutable content can probably be a bit confusing. But if you want truly confusing behavior look at how Java treats numbers wrapped in their object type:<p><pre><code> public class Main{ public static void main(String[] args){ Integer a = 300; Integer b = 300; System.out.printf(&quot; %s == %s: %s\n&quot;, a, b, (a == b)); a = 100; b = 100; System.out.printf(&quot; %s == %s: %s\n&quot;, a, b, (a == b)); } } &#x2F;&#x2F; output 300 == 300: false 100 == 100: true </code></pre> When combined with Java&#x27;s auto-boxing, this can create some serious confusion
评论 #31566742 未加载
评论 #31567979 未加载
评论 #31566950 未加载
评论 #31566746 未加载
评论 #31567004 未加载
raymondhalmost 3 years ago
IMHO this is one of the worst ways to improve your Python skills. Instead, focus on what the normal, robust, beautiful, clear, and idiomatic solutions to problems.<p>There is an enormous amount of quality training material available as well as excellent source code to major projects.<p>Most people would be better off just spending a hour or so with the tutorial at docs.python.org.<p>I get people coming to me asking for a job <i>teaching</i> Python when they don&#x27;t know of some the material in the tutorial. Most have never read the FAQs and aren&#x27;t aware of common solutions to common problems. To me, if you want to build your skills, start there. Get in the habit of reading docs, even boring ones, and occasionally read some of the source code from your favorite libraries.<p>That said, if you&#x27;ve already a very strong Python programmer, one possible use for these snippets is to help you root out any last, misconceptions of minor details.<p>But, you should resist the urge to show-off or use almost any of these &quot;skills&quot;. There are a lot of cute things we can do with chained comparisons, but the only sensible things are: lo &lt;= x &lt;= hi or f(x) == g(x) == h(x). Anything else is too weird for communicating with the human beings. Likewise, you should only use &quot;is&quot; for None tests until you have strong understanding of identity guarantees.
评论 #31572343 未加载
评论 #31572378 未加载
评论 #31572326 未加载