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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Stack Overflow Users Rejoice as Pattern Matching Is Added to Python

119 点作者 srathi超过 4 年前

10 条评论

ketzu超过 4 年前
The problem that arises here is one of pythons scoping and people reading this satire will have a hard time understanding what is happening (if they don&#x27;t follow the links in the article).<p>Taking an example from a reddit comment[1] for the actual pattern matching. The command matched on is a list, and the matching condition in this case is the number of elements in the list.<p><pre><code> command = [&#x27;cmd&#x27;, &#x27;somearg&#x27;] match command: case [nonargaction]: print(f&#x27;We got a non-argument command: {nonargaction}&#x27;) case [action, arg]: print(f&#x27;We got a command with an arg: {action}, {arg}&#x27;) case _: print(&#x27;default, dunno what to do&#x27;) </code></pre> The case gives you named arguments representing the matched strcutre. The scoping problem is, that python will overwrite an existing variable at this scope. Seen here with loops:<p><pre><code> x = 0 for x in range(5): pass print(x) # = 4 m = 5 match m: case x: pass print(x) # = 5 </code></pre> Other examples and applications can be found in one of the relevant PEPs 635 [2] (634 is the specification I believe).<p>[1] <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;programming&#x2F;comments&#x2F;lgqhmj&#x2F;stack_overflow_users_rejoice_as_pattern_matching&#x2F;gmu03eh&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;programming&#x2F;comments&#x2F;lgqhmj&#x2F;stack_o...</a><p>[2] <a href="https:&#x2F;&#x2F;www.python.org&#x2F;dev&#x2F;peps&#x2F;pep-0635&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.python.org&#x2F;dev&#x2F;peps&#x2F;pep-0635&#x2F;</a>
phnofive超过 4 年前
match credulity:<p><pre><code> case woosh: print(“We were starting to worry that we would run out of major language changes to confuse new developers. But thanks to pattern matching, we should be set for another 5-7 years! ”)</code></pre>
评论 #26104428 未加载
jjk166超过 4 年前
While I initially thought this was absurd, looking at the intended use case I think this is actually sensible behavior. While I would probably use a word other than case to further distinguish it from the switch statements of other languages, the fact remains it&#x27;s not a switch statement, and assigning values to variables is the whole point.<p>In the article&#x27;s example, copied below, the programmer is confident that the status code fits the format they are looking for but not what status code corresponds to not found<p><pre><code> NOT_FOUND = 404 match status_code: case 200: print(&quot;OK!&quot;) case NOT_FOUND: print(&quot;HTTP Not Found&quot;) </code></pre> This should be handled by an elif statment. If on the other hand we&#x27;re dealing with<p><pre><code> match ambiguous_response: case status_code: print(&quot;Responded with status code: &quot;,status_code) case { &quot;status_code&quot; : status_code , _ }: print(&quot;Responded with json obj containing status code: &quot;,status_code) </code></pre> doing so with elif statements would be cumbersome at best.
评论 #26110793 未加载
machiaweliczny超过 4 年前
Seriously, who thought such semantics will be useful? What are they smoking?
doublerabbit超过 4 年前
From what I gather from that article is that pattern matching is the new buzzword for Switch Cases?<p>If so, expect an influx of applications misusing switches in replacement of if.
评论 #26100110 未加载
评论 #26102394 未加载
评论 #26100682 未加载
评论 #26103932 未加载
bb101超过 4 年前
Crikey, that&#x27;s going to be confusing. Why not separate capture variables from comparison variables by using tuple-style brackets?<p><pre><code> RED = &quot;red&quot; colour = &quot;green&quot; match colour: case (RED,): print(f&quot;The colour is {RED}&quot;) &gt; The colour is green match colour: case RED: print(f&quot;The colour is {RED}&quot;) &gt; The colour is red</code></pre>
xiaodai超过 4 年前
Pretty funny read
theandrewbailey超过 4 年前
Mapping values to functions was the Pythonic thing for this, right? I guess the solution to that had to be just as unintuitive.
tln超过 4 年前
Constants getting overwritten is such a footgun.<p>Hopefully syntax highlighters and linters will mitigate this.
anotherhue超过 4 年前
Actually a pretty good introduction