|
|
Log in / Subscribe / Register

Hoyt: Structural pattern matching in Python 3.10

Ben Hoyt has published a critical overview of the Python 3.10 pattern-matching feature.

As shown above, there are cases where match really shines. But they are few and far between, mostly when handling syntax trees and writing parsers. A lot of code does have if ... elif chains, but these are often either plain switch-on-value, where elif works almost as well, or the conditions they’re testing are a more complex combination of tests that don’t fit into case patterns (unless you use awkward case _ if cond clauses, but that’s strictly worse than elif).

(Pattern matching has been covered here as well).


to post comments

Hoyt: Structural pattern matching in Python 3.10

Posted Sep 21, 2021 1:02 UTC (Tue) by atnot (guest, #124910) [Link] (2 responses)

I think the big problem with python's pattern matching has nothing to do with the feature itself. Sure the implementation has weaknesses, but those would be easy to overlook if it wasn't so niche. The bigger problem to me is that it's been put into a language where dispatching based on type is unidiomatic and there's no tagged union type to benefit from it either.

I think this is also good news though, because those things can be changed and although they would make python a very different language, it does open up some exciting possibilities.

Hoyt: Structural pattern matching in Python 3.10

Posted Sep 21, 2021 21:45 UTC (Tue) by flussence (guest, #85566) [Link] (1 responses)

Pattern matching is deceptively hard to graft onto a language not built around it because it's not a natural extension of one operator (like a switch is), but about fitting shapes into holes; suddenly you have to invent an entire coherent sublanguage for defining what a hole is. Most language designers unfortunately don't seem to grasp the depth of the problem until they're drowning in it.

Perl tried to skip over implementing switch statements in core and instead ended up with a backported pattern matching construct that nobody could understand - the current version of its own documentation more or less says “Don't use this, we don't know how it works and its behaviour changes every few versions. It looks like this other language but it really isn't, here's 20% of the entire syntax manual dedicated to edge cases if you use it, good luck”

Python's attempting to do the same, run before it walks, and if it doesn't learn from history I feel it's just going to repeat it. Sometimes a switch statement is good enough.

Hoyt: Structural pattern matching in Python 3.10

Posted Sep 27, 2021 18:30 UTC (Mon) by jezuch (subscriber, #52988) [Link]

Luckily, designers of Java are aware of this, and the way they are adding pattern matching is long and tortuous, and they're thinking about it *really* hard. Because it's not just "hey, let's just do it!!" like, seemingly, Python did, but it's a major transformation of the language and its type system to match (no pun intended).

I can't wait until I can lay my hands on the results! (Although there's a real possibility that I'll switch to Rust before that happens ;) )

Hoyt: Structural pattern matching in Python 3.10

Posted Sep 21, 2021 6:46 UTC (Tue) by oldtomas (guest, #72579) [Link] (5 responses)

It's somewhat eerie to see Python rediscovering things the Lisps have converged on since... 1970s? 1990s?

FWIW, Common Lisp(s) and Schemes have both: `cond' and `case'. Perhaps for a reason.

Hoyt: Structural pattern matching in Python 3.10

Posted Sep 21, 2021 12:30 UTC (Tue) by mathstuf (subscriber, #69389) [Link] (4 responses)

I don't think it is Python "rediscovering" them so much as realizing their overall value even in a dynamic language. List comprehensions come from Haskell, so Guido is certainly at least aware of "advanced" languages.

Hoyt: Structural pattern matching in Python 3.10

Posted Sep 21, 2021 17:49 UTC (Tue) by marcH (subscriber, #57642) [Link] (3 responses)

> List comprehensions come from Haskell,

Apparently 20 years older than Haskell

https://en.wikipedia.org/wiki/List_comprehension#History

Hoyt: Structural pattern matching in Python 3.10

Posted Sep 24, 2021 19:42 UTC (Fri) by jond (subscriber, #37669) [Link] (2 responses)

I read the comment to mean "Python's list comprehensions come from Haskell", as in, Haskell's specific implementation of list comprehensions are the direct inspiration for Python's.

Hoyt: Structural pattern matching in Python 3.10

Posted Sep 26, 2021 8:04 UTC (Sun) by marcH (subscriber, #57642) [Link] (1 responses)

I don't know Haskell but its syntax for list comprehension does not look like Python's https://wiki.haskell.org/List_comprehension

I don't see what can be "specific implementation" besides the syntax. It's a pretty simple (as in: good) idea.

Hoyt: Structural pattern matching in Python 3.10

Posted Sep 27, 2021 13:04 UTC (Mon) by mathstuf (subscriber, #69389) [Link]

"Specific implementation" may be too strong. But I wouldn't be surprised if Guido encountered the concept *via* Haskell, no matter the feature's specific history (just as one can learn about `{}` languages via Java despite it being far from the first to use such syntax). As for the syntax, it's about as close as one can get given the restrictions on each. Python is going to have parenthesis and Haskell is going to prefer symbols.


Copyright © 2021, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds