Stack Overflow Users Rejoice as Pattern Matching Is Added to Python
brennan.io
brennan.io
Taking an example from a reddit comment[1] for the actual pattern matching. The command matched on is a list, and the matching condition in this case is the number of elements in the list.
command = ['cmd', 'somearg']
match command:
case [nonargaction]:
print(f'We got a non-argument command: {nonargaction}')
case [action, arg]:
print(f'We got a command with an arg: {action}, {arg}')
case _:
print('default, dunno what to do')
The case gives you named arguments representing the matched strcutre. The scoping problem is, that python will overwrite an existing variable at this scope.
Seen here with loops: x = 0
for x in range(5):
pass
print(x) # = 4
m = 5
match m:
case x: pass
print(x) # = 5
Other examples and applications can be found in one of the relevant PEPs 635 [2] (634 is the specification I believe).[1] https://www.reddit.com/r/programming/comments/lgqhmj/stack_o...
case woosh:
print(“We were starting to worry that we would run out of major language changes to confuse new developers. But thanks to pattern matching, we should be set for another 5-7 years! ”)In the article's example, copied below, the programmer is confident that the status code fits the format they are looking for but not what status code corresponds to not found
NOT_FOUND = 404
match status_code:
case 200:
print("OK!")
case NOT_FOUND:
print("HTTP Not Found")
This should be handled by an elif statment. If on the other hand we're dealing with match ambiguous_response:
case status_code:
print("Responded with status code: ",status_code)
case { "status_code" : status_code , _ }:
print("Responded with json obj containing status code: ",status_code)
doing so with elif statements would be cumbersome at best.If so, expect an influx of applications misusing switches in replacement of if.
Pattern matching is a great language feature but I've been watching the development of it for Python for a while and have felt nothing but horror.
If you want a great example of how it can be useful and how clear and elegant it can be, check out how it's done in Elixir [0].
There are many other languages that implement it in great ways, I'm sure others will comment on them.
[0] https://elixir-lang.org/getting-started/pattern-matching.htm...
[1] https://www.reddit.com/r/programming/comments/lgqhmj/stack_o...
If you want to see what it _really_ should look like, see Erlang, and then look at Prolog.
Structural pattern matching is a lot more than just switch/case, and I while I think pythons implementation looks imperfect, it will still be an amazing boon to the language.
There are a few gotcha-edge cases, but they are of the kind that are obvious once you've encountered them once.
I already know a couple dozen functions in my projects that I'm going to refactor to a fraction of their current length and improve the readability of at the same time as soon as this lands.
RED = "red"
colour = "green"
match colour:
case (RED,):
print(f"The colour is {RED}")
> The colour is green
match colour:
case RED:
print(f"The colour is {RED}")
> The colour is redHopefully syntax highlighters and linters will mitigate this.