Structural pattern matching in Python 3.10

# · 🔥 273 · 💬 147 · 2 years ago · benhoyt.com · chmaynard · 📷
Summary: Python 3.10, which is due out in early October 2021, will include a large new language feature called structural pattern matching. At a recent local Python meetup, a friend was presenting some of the new features in Python 3.8 and 3.9, and afterwards we got to talking about the pattern matching feature coming in Python 3.10. The benefits need to be discussed in light of real-world code: the kind of code people use Python for on a day-to-day basis, not just the fairly contrived examples in the "Motivation" section of the PEP. Part of what I want to do here is evaluate some real code and see how much pattern matching improves it. Let's take a brief look at what structural pattern matching in Python looks like. Despite my skepticism, I'm trying to be fair: these examples do look nice, and even if you haven't read the pattern matching spec, it's reasonably clear what they do - with the possible exception of the match args magic. There are 1594 uses of elif, which again is 0.7%. Below are a couple of cases I saw which might benefit from pattern matching. Overall I'm a bit pessimistic about structural pattern matching in Python.
Structural pattern matching in Python 3.10



Send Feedback | WebAssembly Version (beta)