LWN: Comments on "A more generalized switch statement for Python?" https://lwn.net/Articles/693482/ This is a special feed containing comments posted to the individual LWN article titled "A more generalized switch statement for Python?". en-us Tue, 14 Oct 2025 10:43:41 +0000 Tue, 14 Oct 2025 10:43:41 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net A more generalized switch statement for Python? https://lwn.net/Articles/694006/ https://lwn.net/Articles/694006/ flussence <div class="FormattedComment"> The very least they could do here is learn to not repeat the mistakes Perl 5 did in trying to add a switch construct to the language.<br> <p> The examples given all look very *pretty*, which is what one would expect Python culture to focus on, but they're harder to *understand*.<br> </div> Sun, 10 Jul 2016 20:14:09 +0000 A more generalized switch statement for Python? https://lwn.net/Articles/693994/ https://lwn.net/Articles/693994/ HIGHGuY <div class="FormattedComment"> Pattern matching in those languages is great... because of the very strong typing (and automatic type deduction).<br> Putting this kind of matching with python's duck-typing is like trying to have your cake and eat it too. It's just going to cause even more subtle bugs that are hard to diagnose or find.<br> </div> Sun, 10 Jul 2016 18:19:19 +0000 A more generalized switch statement for Python? https://lwn.net/Articles/693940/ https://lwn.net/Articles/693940/ marcH <div class="FormattedComment"> Once you've seen in action ML-like pattern matching in some random language (from the ML family or not) and caught actual bugs thanks to it, then people claiming that if/elif/else is "The One Obvious Way to Do It" really sound like they're from the distant past. Maybe still trying to do large-scale development in assembly or something.<br> <p> Quite surprising from Python: an otherwise fairly high-level language.<br> </div> Sat, 09 Jul 2016 04:28:59 +0000 A more generalized switch statement for Python? https://lwn.net/Articles/693893/ https://lwn.net/Articles/693893/ nybble41 <div class="FormattedComment"> <font class="QuotedText">&gt; The if/elif chain says "try this, then that, now something else". There's no implication that the tests are all looking at the same subject ... With a switch statement, however, the subject is stated once, at the top of the statement. The checks are then listed one after the other, and they are all by definition checks against the subject expression.</font><br> <p> This is true of switch statements in C, but not every other language. For example, in Haskell the top-level patterns are all matched against a single value, but you can also add "guards" which add arbitrary conditions and even nested pattern matches:<br> <p> case x of<br> (4, y) -&gt; "case 1"<br> (x, 7) -&gt; "case 2"<br> (x, y) | x &lt; y -&gt; "case 3"<br> _ | z == True -&gt; "case 4"<br> _ | [z, w] &lt;- list -&gt; "case 5"<br> <p> Haskell also has "view patterns" as a language extension which allow things like matching on dictionaries:<br> <p> case dict of<br> (M.lookup "key" -&gt; Just value) -&gt; print value<br> _ -&gt; putStrLn "No match!"<br> <p> The left-hand side of the view pattern is an arbitary function which is given the value at that position as a parameter. The right-hand side is a pattern matched against the function's return value. Of course, in this case you could just as easily write `case M.lookup "key" dict of ...`; it becomes more useful when you want to match against multiple keys or use the view pattern in a pattern synonym.<br> <p> F# has a similar guard syntax as well as "active patterns", which are similar to Haskell's view patterns combined with pattern synonyms.<br> </div> Fri, 08 Jul 2016 17:47:44 +0000 A more generalized switch statement for Python? https://lwn.net/Articles/693873/ https://lwn.net/Articles/693873/ gasche <div class="FormattedComment"> Algebraic type and pattern-matching were introduced in the 1970s by the functional language Hope, and were quickly adopted by the ML languages (SML, OCaml, Haskell, F#, etc.; with a graceful adaptation to the object-oriented programming as sealed classes in Scala). I find that the discussions related in this article give the impression of the Python community as rather closed-minded as far as language design is concerned. For an interesting take on the form of programming language illiteracy fostered by self-contained one-language communities, see<br> <p> Why I hate advocacy<br> Mark-Jason Dominus, 2000<br> <a href="http://www.perl.com/pub/2000/12/advocacy.html">http://www.perl.com/pub/2000/12/advocacy.html</a><br> <p> <p> </div> Fri, 08 Jul 2016 15:44:49 +0000