LWN: Comments on "Python structural pattern matching morphs again" https://lwn.net/Articles/838600/ This is a special feed containing comments posted to the individual LWN article titled "Python structural pattern matching morphs again". en-us Sat, 11 Oct 2025 19:07:15 +0000 Sat, 11 Oct 2025 19:07:15 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net Python structural pattern matching morphs again https://lwn.net/Articles/841283/ https://lwn.net/Articles/841283/ jezuch <div class="FormattedComment"> <font class="QuotedText">&gt; lifetimes. (...) Suggesting Rust, on the other hand, requires learning a new paradigm that affects pretty much every line of code.</font><br> <p> To be fair, this is inferred most of the time (just like types and some other things); it&#x27;s only needed (AFAICT, possibly wrongly) when you return a reference from a function which might also be one of the arguments. At least this was the moment it &quot;clicked&quot; for me and I learned to stop wondering WTH I need those annotations for and love lifetimes.<br> <p> And besides, I think that many experienced programmers already know this &quot;paradigm&quot; without knowing it, so to speak.<br> </div> Sat, 26 Dec 2020 19:55:08 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/841114/ https://lwn.net/Articles/841114/ mathstuf <div class="FormattedComment"> To be fair, most of the app is slinging around HTTP requests to hosting providers (GraphQL/REST), parsing JSON, and forking off thousands of `git` processes :) . But the ability to just stick a `.par_iter()` in one place and get magic parallelism when running dozens of checks on dozens of commits (it&#x27;s NxM) makes it amazingly easy. I could squeeze some more parallelism out of it with ticketed locks so that projects can be processed in parallel, but it hasn&#x27;t been necessary to do that kind of stuff.<br> <p> I&#x27;ve talked about the project before on here, but it all lives here for anyone curious:<br> <p> <a href="https://gitlab.kitware.com/utils">https://gitlab.kitware.com/utils</a><br> <p> All of the &quot;rust&quot; and &quot;ghostflow&quot; repositories; the &quot;source-formatters&quot; repo is relevant in that it&#x27;s something we point our configurations at to do formatting verification and fixing. &quot;git-import-third-party&quot; is relevant for anyone wanting to use the &quot;third party&quot; check to make sure vendored code is not modified outside of dedicated patch tracking mechanisms.<br> </div> Tue, 22 Dec 2020 21:43:18 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/841104/ https://lwn.net/Articles/841104/ mpr22 <div class="FormattedComment"> <font class="QuotedText">&gt; It&#x27;s also performant enough that we just deploy the debug build</font><br> <p> This is really cool, and I can&#x27;t think of much higher praise for a language&#x27;s performance :)<br> </div> Tue, 22 Dec 2020 17:27:48 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/841103/ https://lwn.net/Articles/841103/ mathstuf <div class="FormattedComment"> I think it depends heavily upon the kinds of code you&#x27;re writing. I worked on a system in Rust that just could not go down (it manages/directs our development workflow). Rust&#x27;s emphasis on error handling and consideration of what can go wrong means that if anything does go wrong, it goes to a log instead of crashing the program. There&#x27;s very little lifetime usage in types throughout the 50k+ lines of code in it. They have allowed us to make the parts that need to be fast parallelisable without having to do manual checks of shared memory or the like.<br> <p> This system has been running for over 4 years now, gained support for non-`master` default branches in July, updated its GraphQL schema usage in June, added a couple of small features this past spring, a few trickling in over time (as they do), and hasn&#x27;t really had an &quot;oh shit&quot; moment in over a year (I&#x27;ve stopped scrolling the history; there have been 2 that I can remember that weren&#x27;t &quot;gitlab or github changed something on us&quot; (those are just error logs, not crashes) and they were logic errors possible in any language). It&#x27;s also performant enough that we just deploy the debug build so the backtraces are useful (when needed).<br> <p> If you&#x27;re crafting your own data structures, yes, Rust is going to be a tough sell over more convenient languages (though I argue that it is still worth it). But if you&#x27;re writing code that needs to be correct in production, performance is of at least some note, and where threading can really help out there, Rust is definitely the top of my list for what to implement it in.<br> </div> Tue, 22 Dec 2020 17:16:43 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/841037/ https://lwn.net/Articles/841037/ intgr <div class="FormattedComment"> <font class="QuotedText">&gt; Not to mention python tends to prefer duck typing a lot and most of the examples are counter to that.</font><br> <p> I&#x27;ve seen lots of backpedalling on that approach. It&#x27;s been a slow transition, but starting with the introduction of Abstract Base Classes (ABC), and more importantly, type hints, the whole community has become more accepting of nominative typing.<br> <p> Data types that used to be hidden as internal implementation details are more frequently being exposed as public types, etc.<br> <p> It would be possible to achieve &quot;duck typing&quot; type hints with protocols (structural typing) as well, but ironically the current implementation in Python is problematic.<br> <p> </div> Tue, 22 Dec 2020 10:27:38 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/841036/ https://lwn.net/Articles/841036/ intgr <div class="FormattedComment"> I think the best feature of Rust is also the worst one: lifetimes. Suggesting to adopt a &quot;classical&quot; language such as Java or Go to a team not yet familiar to it isn&#x27;t a huge jump.<br> <p> Suggesting Rust, on the other hand, requires learning a new paradigm that affects pretty much every line of code. As good as that paradigm may be, it&#x27;s a hard sell if the old paradigm works &quot;well enough&quot;.<br> <p> </div> Tue, 22 Dec 2020 10:08:24 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/840320/ https://lwn.net/Articles/840320/ smitty_one_each <div class="FormattedComment"> As far as I can tell, the thrust of this change is to move a bunch of logic into the type system.<br> <p> This seems a trend in programming languages away from procedural and into more abstract mathematical realms.<br> <p> The genius of Python is in striking a balance between &quot;featuritis&quot; and adding stuff for stuff&#x27;s sake to the language and really hashing out the details that are going to matter in the long run.<br> <p> Structural pattern matching seems an esoteric tool that one will gladly reach for when needed but mostly ignore in casual work.<br> </div> Wed, 16 Dec 2020 13:11:00 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/839729/ https://lwn.net/Articles/839729/ papik I was thinking... Python already has an assign-ish keyword "as". Maybe it is more pythonic something like:<br> <pre> match ptlike: as Point2d(x, y): return Point3d(x, y, 0) </pre> But in my opinion there are too many colons... Thu, 10 Dec 2020 17:30:10 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/839609/ https://lwn.net/Articles/839609/ HelloWorld <div class="FormattedComment"> Pattern matching isn&#x27;t “some feature” that is “now fashionable”. It&#x27;s a feature that has been around for 50 years at this point and has a solid mathematical foundation. It&#x27;s frankly an embarrassment that Python still has to do without it to this day.<br> </div> Thu, 10 Dec 2020 12:32:25 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/839608/ https://lwn.net/Articles/839608/ HelloWorld <div class="FormattedComment"> What really bad features does rust have?<br> </div> Thu, 10 Dec 2020 12:26:32 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/839215/ https://lwn.net/Articles/839215/ dbaker <div class="FormattedComment"> <font class="QuotedText">&gt; I cannot believe that somebody wrote those two words, by each other, unironically</font><br> <p> I actually was trying to be a little snarky, I tend to think that Rust and Zig have some nice features, but also some really bad ones :)<br> </div> Mon, 07 Dec 2020 17:51:08 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/839080/ https://lwn.net/Articles/839080/ iustin <div class="FormattedComment"> Well, you could also say that the typing annotations are not enforce, so their usefulness is limited. But I&#x27;ve seen real bugs being found by the type annotations, so just because Python+pattern matching is not Haskell&#x27;s one, I wouldn&#x27;t say it&#x27;s not useful.<br> <p> At least least, it gives a more succint view on what the author of the code thought/how they saw the code flow should happen, (the same way as type annotations show what they thought of the involved types), so in my book they&#x27;re very useful.<br> </div> Sun, 06 Dec 2020 20:47:56 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/839078/ https://lwn.net/Articles/839078/ ehiggs <div class="FormattedComment"> Python lacks the sum types that Haskell and Rust have which make the pattern matching exhaustive. So the usefulness appears to be quite limited. <br> </div> Sun, 06 Dec 2020 19:59:32 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/839037/ https://lwn.net/Articles/839037/ iustin <div class="FormattedComment"> <font class="QuotedText">&gt; I write python pretty much all day. I&#x27;m excited to having this. maybe I&#x27;m the only one.</font><br> <p> As a Haskell programmer in my free time who writes mostly Python at work, I&#x27;m very excited by this, and after all the typing annotations added to Python (which I just love), I&#x27;m quite surprised that a simple pattern matching results in &quot;I don&#x27;t want it in _my_ Python&quot;.<br> <p> &lt;snip lots of explanations because it won&#x27;t convince people who made up their mind&gt;<br> <p> … but if Python doesn&#x27;t add it, in one form or another, I think it&#x27;s a loss for the Python language. Not for the community, since yes, it would raise the complexity of the language a tiny, tiny bit.<br> </div> Sat, 05 Dec 2020 21:35:09 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/838997/ https://lwn.net/Articles/838997/ Deleted user 129183 <div class="FormattedComment"> <font class="QuotedText">&gt; sexy languages</font><br> <p> I cannot believe that somebody wrote those two words, by each other, unironically.<br> <p> <font class="QuotedText">&gt; rust and zig have pattern matching</font><br> <p> Oh, sure, those languages are fashionable right now, but in maybe, 15 years, they and their features will probably go out of fashion and style – just like, 15 years ago every language had to be object-oriented and have exceptions as the preferred method of the error handling, but apparently these things are now Evil, and new languages like Holy Rust do not have them or have them in a greatly reduced form – now procedural programming and result types are The Way.<br> <p> But Python is a language that’s by now firmly established as one of the “major languages”, and we will probably still use it widely in 15 years (unless it goes the way of Perl/Raku). There’s no real reason to force some feature on it because it’s now fashionable, but when implemented badly, it could be an ugly legacy wart in the near future.<br> </div> Fri, 04 Dec 2020 21:42:31 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/838931/ https://lwn.net/Articles/838931/ jezuch <div class="FormattedComment"> It is much better if a feature is present in the language from the beginning, because then it is part of a consistent design. Adding a big feature like this to an established language risks making it a huge, inconsistent wart. And it looks to me to be the case with match statement in Python.<br> <p> Mind you, exactly the same thing is happening in Java right now, where they are trying to extend the switch statement to support pattern matching. The issues are also almost identical. The difference is that in Java they envision it as part of a bigger picture - not just pattern matching, but turning the entire type system around to be more like algebraic types. (See for example the recent addition of Record types.) For this reason this is not a simple task and the work has been going on for a couple of years now. I like to read the discussions on their mailing list and, sorry to say that, the way it happens in Python looks terribly amateurish in comparison :) I&#x27;m actually in awe of the projects Valhalla and Amber members.<br> </div> Fri, 04 Dec 2020 09:12:50 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/838900/ https://lwn.net/Articles/838900/ dbaker <div class="FormattedComment"> Also, sexy languages like rust and zig have pattern matching. Frankly a lot of the arguments being made here could probably have been made about other functional elements like comprehensions, you can just write a loop or use filter() and map(), but a comprehension is a lot easier to use once you get the hang of it.<br> <p> I write python pretty much all day. I&#x27;m excited to having this. maybe I&#x27;m the only one.<br> </div> Thu, 03 Dec 2020 18:20:15 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/838898/ https://lwn.net/Articles/838898/ ms <div class="FormattedComment"> <font class="QuotedText">&gt; where does it end? Would we have case x + y: print(x, y)?</font><br> <p> Haskell has n+k patterns (albeit behind an option - they&#x27;re off by default), though one of the args must be a literal constant. But, err, I suspect Prolog and the halting-problem in general is where it would end if you took this to its logical conclusion.<br> </div> Thu, 03 Dec 2020 17:55:19 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/838893/ https://lwn.net/Articles/838893/ nybble41 <div class="FormattedComment"> <font class="QuotedText">&gt; Those languages largely use flexible (polymorphic / inferred) types too.</font><br> <p> Inferred static types are very different from dynamic types, even if they appear superficially similar at the source level. Even with polymorphism, the type of the value being matched must be known in advance for each particular instance. The compiler can use this information to generate jump tables rather than long if/else chains and to statically test for exhaustiveness in the match patterns. The simplest pattern matches (e.g. destructuring a product type) can be optimized out altogether.<br> <p> You can get approximately the same behavior with runtime tests in dynamic languages, but the performance cost is considerable.<br> </div> Thu, 03 Dec 2020 17:32:14 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/838894/ https://lwn.net/Articles/838894/ NYKevin <div class="FormattedComment"> Python does not meaningfully distinguish between class instantiation and function calls. If you allow destructuring the function call operator, where does it end? Would we have case x + y: print(x, y)?<br> <p> IMHO this whole thing would make a lot more sense if it *just* supported the regular unpacking syntax a la PEP 448, and (maybe) a very conservative extension for matching simple enum constants. Everything else is just going to require too many weird special cases to get right.<br> </div> Thu, 03 Dec 2020 17:29:39 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/838892/ https://lwn.net/Articles/838892/ mathstuf <blockquote>Not to mention python tends to prefer duck typing a lot and most of the examples are counter to that.</blockquote> Agreed. If it allowed something like this, I think it'd be way more powerful: <pre> match ptlike: case c(x=x, y=y): return Point3d(x, y, 0) </pre> to match on *any* class with an x and y attribute (rather than `hasattr` checks or an `AttributeError` catch)? But maybe the AttributeError is just more Pythonic anyways… This makes me think…is the "in front of parens" token *always* looked up in the scope and not allowed to be a captured variable? If so, it's kind of odd that even it gets to be special too… Thu, 03 Dec 2020 17:12:27 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/838854/ https://lwn.net/Articles/838854/ rsidd <div class="FormattedComment"> Yes, a recent trend if by &quot;last few years&quot; you mean &quot;since the 1970s&quot; (ML). Python has lifted several things from functional languages. This is another and is extremely useful. Those languages largely use flexible (polymorphic / inferred) types too. <br> </div> Thu, 03 Dec 2020 10:41:35 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/838849/ https://lwn.net/Articles/838849/ lxsameer <div class="FormattedComment"> Totally agree, The rational behind it doesn&#x27;t make sense either, In order to utilize pattern matching a language has to have a better type system than what Python has at the moment and beside that they&#x27;re trying to address some issues that Protocols/Traits are better solutions.<br> <p> To me, it seems that because pattern matching and type systems are a trend in past few years they want them in Python otherwise it&#x27;s violates Python&#x27;s Zen and would be mess.<br> </div> Thu, 03 Dec 2020 09:44:34 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/838847/ https://lwn.net/Articles/838847/ rsidd This is not akin to the C switch statement as much as to the match construct in the ML family (eg <a href="https://caml.inria.fr/pub/docs/oreilly-book/html/book-ora016.html">ocaml</a>). Immensely useful. There is something similar in Haskell, and it is implemented as a macro in <a href="https://kmsquire.github.io/Match.jl/latest/">Julia</a>. Thu, 03 Dec 2020 09:17:15 +0000 Python structural pattern matching morphs again https://lwn.net/Articles/838837/ https://lwn.net/Articles/838837/ logang <div class="FormattedComment"> IMO the match proposal is counter to almost half of the zen of python. Specifically these ideals:<br> <p> <font class="QuotedText">&gt; Explicit is better than implicit.</font><br> <font class="QuotedText">&gt; Simple is better than complex.</font><br> <font class="QuotedText">&gt; Flat is better than nested.</font><br> <font class="QuotedText">&gt; Readability counts.</font><br> <font class="QuotedText">&gt; Special cases aren&#x27;t special enough to break the rules.</font><br> <font class="QuotedText">&gt; There should be one-- and preferably only one --obvious way to do it.</font><br> <font class="QuotedText">&gt; Although never is often better than *right* now.</font><br> <font class="QuotedText">&gt; If the implementation is hard to explain, it&#x27;s a bad idea.</font><br> <p> Not to mention python tends to prefer duck typing a lot and most of the examples are counter to that.<br> <p> If accepted, I&#x27;d almost certainly ask my colleagues not to use it and suggest traditional methods during review.<br> </div> Thu, 03 Dec 2020 01:58:27 +0000