Personal Log »

Python's pattern matching

So Python has pattern matching in 3.10:

# the syntax highlighter doesn't know what to do
# with this new construct ;)
match point:
    case Point(x, y) if x == y:
        print(f"Y=X at {x}")
    case Point(x, y):
        print(f"Not on the diagonal")

And I think it looks fantastic!

I always found the chains of elif a bit awkward, but I guess that was part of what made Python simple yet powerful.

And this feature goes beyond that –take a look at the tutorial, search for value extractors-, and being this one of the features I liked most in my first contact with Scala, this is probably one of the few recent changes in Python that I find truly exciting (I must admit I didn’t like type hints initially, but now I think they are a very good idea).

It is still an alpha release, and it will need some time until it lands my Linux distribution (this Debian box is still on 3.7.3), but if I go back to do Python professionally, looks like it could be a lot of fun!

Would you like to discuss the post? You can send me an email!