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.

This is valid Python syntax

133 pointsby ankitg12almost 2 years ago

22 comments

BoppreHalmost 2 years ago
I don&#x27;t find that so bad. They look complicated but behave as expected, and would only be a problem if you inherit a code base from a lone wolf without common sense.<p>Python has plenty of warts[1], but I don&#x27;t think the syntax is one of them (yet). Some of the more concerning warts:<p><pre><code> # Mutable defaults in function parameters. def fn(arg=[]): arg.append(1) print(arg) fn() # [1] fn() # [1, 1] fn() # [1, 1, 1] # Loop variables are shared across executions. lambda_list = [lambda: print(i) for i in range(5)] for fn in lambda_list: fn() # 4 # 4 # 4 # 4 # 4 # Iterating over an exhausted generator yields an empty list. numbers = (i for i in range(5)) assert 1 in numbers print(list(numbers)) # [] # I hate this one because it could have easily been an error, it&#x27;s # hard to track down, and lots of stdlib functions return generators. # Chained operators can sometimes be very unintuitive. should_be_ascending = True assert 1 &lt; 2 == should_be_ascending # AssertionError because it was interpreted as # `(1 &lt; 2) and (2 == should_be_ascending)` </code></pre> [1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;satwikkansal&#x2F;wtfpython">https:&#x2F;&#x2F;github.com&#x2F;satwikkansal&#x2F;wtfpython</a>
评论 #36282774 未加载
评论 #36288003 未加载
评论 #36282531 未加载
评论 #36282579 未加载
评论 #36282728 未加载
评论 #36285678 未加载
评论 #36284861 未加载
评论 #36283707 未加载
评论 #36284537 未加载
ptxalmost 2 years ago
Strange article. I thought it was going to show that these features interact in some unexpected way that causes weird bugs, but it all seems to work as expected.<p>So the conclusion, I guess, is that code doesn&#x27;t make a whole lot of sense when it uses every available feature for no particular reason and foreign languages for variables names?
评论 #36281239 未加载
评论 #36284802 未加载
kkirschealmost 2 years ago
I learned about Python dev mode in this, thanks!<p>For anyone else:<p><a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;devmode.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;devmode.html</a>
pedrovhbalmost 2 years ago
&gt; While not as powerful as destructuring in other languages (I still wish we could unpack dicts as JS does with objects)<p>Like this?<p><pre><code> In [1]: a = {1: 10, 2: 20} In [2]: b = {3: 30, **a} In [4]: b Out[4]: {3: 30, 1: 10, 2: 20} </code></pre> Or pattern matching?<p><pre><code> In [6]: match b: ...: case {3: somethingelse, 1: foo}: ...: print(somethingelse, foo) 30 10</code></pre>
评论 #36282919 未加载
评论 #36283446 未加载
randallsquaredalmost 2 years ago
&gt; <i>if you use &quot;_&quot; in hex, and interpolate it with a 2 decimal precision</i><p>...or if you don&#x27;t. The &#x27;_&#x27; doesn&#x27;t change anything or add anything, here.
lysecretalmost 2 years ago
Biggest footgun in python IMO are mutable default parameters. It amazes me that someone thought this was a good idea.
评论 #36284656 未加载
评论 #36283047 未加载
评论 #36284104 未加载
评论 #36288044 未加载
评论 #36284890 未加载
userbinatoralmost 2 years ago
That example with the Chinese characters brings up a point that seems to have gotten lost or actively suppressed, likely by those with other motives: what may look &quot;unreadable&quot; to you may in fact be something that is perfectly normal to billions of others. Yet why are people constantly told to emphasise &quot;readability&quot; in the code they write, which really refers to a lowest-common-denominator dumbing down approach? Shouldn&#x27;t the onus be on the reader to educate oneself sufficiently in the language to understand code that one thinks is &quot;unreadable&quot;? Moreover, unlike natural languages, programming languages are defined exactly and systematically, so there is no ambiguity whether a piece of code is valid or what it does.<p>What is &quot;unreadable&quot; to some could be perfectly readable to others; and what is readable to some could still be readable but unnecessarily verbose and tedious to read to others. We shouldn&#x27;t let how we write code be dictated by those who barely know the language and want to drag us down to their level.
评论 #36287330 未加载
dinomalmost 2 years ago
Lol, and they say Perl is bad.<p><a href="https:&#x2F;&#x2F;www.perl.com&#x2F;pub&#x2F;2000&#x2F;01&#x2F;10PerlMyths.html&#x2F;#Perl_looks_like_line_noise" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.perl.com&#x2F;pub&#x2F;2000&#x2F;01&#x2F;10PerlMyths.html&#x2F;#Perl_look...</a><p>This isn&#x27;t meant as flamebait, I&#x27;m just pointing out you can write obfuscated code in any language.
2halmost 2 years ago
&gt; a, b = b, a<p>&gt; (don&#x27;t know why this is so famous, I never, ever used that in prod)<p>This allows you to sort an array in place. It&#x27;s heavily used in Go:<p><a href="https:&#x2F;&#x2F;godocs.io&#x2F;sort#example-package" rel="nofollow noreferrer">https:&#x2F;&#x2F;godocs.io&#x2F;sort#example-package</a>
评论 #36283143 未加载
评论 #36282444 未加载
评论 #36281326 未加载
评论 #36283387 未加载
bettercallsaladalmost 2 years ago
We had a dev in our team who would love to use these one liner cryptic expressions and challenge the minds of code reviewers.<p>It was all fun until he left and others had to refactor bunch of code. One liners like that often have very little benefits IMHO.
bandramialmost 2 years ago
As a Perl guy none of that is even slightly distressing to me
da39a3eealmost 2 years ago
This is neat!<p><pre><code> img = [&quot;r&quot;, &quot;g&quot;, &quot;b&quot;] * 10_000_000 iterator= iter(img) groups= zip(iterator, iterator, iterator) </code></pre> I&#x27;ve been writing Python for 15 years and it had never occurred to me that I could do it like that. Honestly I think I&#x27;d have reached for more_itertools.chunked.<p>(FWIW In R I think I used to set an array dimension on the vector and process rows.)
macintuxalmost 2 years ago
Unpacking used to be more powerful. I was mostly oblivious to the Python community when I started working on a project and discovered that you could unpack tuples in function heads, like Erlang, and started using that frequently.<p>Then I discovered I was using Python 2 and it was EOL, and that Python 3 had dropped support for that “because no one uses it.”<p>Maddening.
1024corealmost 2 years ago
Python is slowly morphing into Perl... :-D
actionfromafaralmost 2 years ago
Someone™ should make a Python-to-Python compiler which converted all normal constructs into weirdness like this.
评论 #36285506 未加载
guenthertalmost 2 years ago
&gt; Inverting variables:<p>Non-native speaker here and even after 20+years every once in a while it bites, but I know that as &#x27;swapping variables&#x27;. Where does the inverting come from? Related to the &#x27;inverting a tree&#x27;?
评论 #36284986 未加载
评论 #36287977 未加载
svilen_dobrevalmost 2 years ago
most of these are loooong time there. list[:] replacement is since ever. (used for reversing a list, when there wasnt .reverse method)<p>But my beloved one, is the sequence of these changes, in time:<p>ver.134: x = (&#x27;a&#x27;, 2)<p>some change in requirements made 2nd value unneeded, so it became:<p>v167: x = (&#x27;a&#x27;,)<p>which is one day, &quot;optimized&quot; to:<p>v193: x = (&#x27;a&#x27;)<p>Funny thing is, and x[0] will still deliver &#x27;a&#x27; as it was before. Now if that &#x27;a&#x27; is not constant but some parameter, and it&#x27;s not 1 character long string..
评论 #36293234 未加载
dec0dedab0dealmost 2 years ago
Oh man, I seriously just wretched at the idea of putting an import in a lambda. That was sick, and awesome.
评论 #36281251 未加载
charcircuitalmost 2 years ago
数字 is numeral. It doesn&#x27;t make sense as a translation for numbers in that function.
EddieJLSHalmost 2 years ago
Fun syntax gotcha that I saw recently was a YAML MAC address that were all numbers like 11:22:33:11 which Python was parsing as a sexagesimal number so some libraries were throwing type errors
jonnycomputeralmost 2 years ago
God, is this going to be what I see in my next python coding interview?
评论 #36285706 未加载
评论 #36284309 未加载
bionhowardalmost 2 years ago
Lol, your definition of “valid syntax” is wayyy different from mine. I wouldn’t ever use crap like that, it’s too unreadable to be “valid syntax” even if the interpreter can handle it. Can we all collectively agree to include our own brains in the definition of “valid code?”
评论 #36287002 未加载