match credulity:
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! ”)
While I initially thought this was absurd, looking at the intended use case I think this is actually sensible behavior. While I would probably use a word other than case to further distinguish it from the switch statements of other languages, the fact remains it's not a switch statement, and assigning values to variables is the whole point.
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.Seriously, who thought such semantics will be useful? What are they smoking?
From what I gather from that article is that pattern matching is the new buzzword for Switch Cases?
If so, expect an influx of applications misusing switches in replacement of if.
Crikey, that's going to be confusing. Why not separate capture variables from comparison variables by using tuple-style brackets?
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 red
Pretty funny read
Mapping values to functions was the Pythonic thing for this, right? I guess the solution to that had to be just as unintuitive.
Constants getting overwritten is such a footgun.
Hopefully syntax highlighters and linters will mitigate this.
Actually a pretty good introduction
The problem that arises here is one of pythons scoping and people reading this satire will have a hard time understanding what is happening (if they don't follow the links in the article).
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.
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: 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...
[2] https://www.python.org/dev/peps/pep-0635/