Python already has something accessible and similar in it's tuple unpacking. For example it allows:
a,b,c = (1,2,3)
Here the variables are being filled by values from the tuple based purely on their position within it.
Pattern matching takes this a few steps further by allowing you to mix known values into things and combine dispatch from those values with variable assignment.
For example, imagine that Python had the following Python expression give a true/false value based on if the elements match:
"test", a, b = ("test", 1, 2) # would evaluate to True and bind a=1, b=2
"test", a, b = ("nope", 1, 2) # would evaluate to False and would not bind a or b
This can clean up a surprising amount of code, especially if you structure a case statement around it.
The double-whammy is when you allow a function to be defined multiple times so long as each definition has a different pattern match in the variable declaration. The Erlang article I linked to covers this topic pretty well.
Python already has something accessible and similar in it's tuple unpacking. For example it allows:
a,b,c = (1,2,3)
Here the variables are being filled by values from the tuple based purely on their position within it.
Pattern matching takes this a few steps further by allowing you to mix known values into things and combine dispatch from those values with variable assignment.
For example, imagine that Python had the following Python expression give a true/false value based on if the elements match:
"test", a, b = ("test", 1, 2) # would evaluate to True and bind a=1, b=2
"test", a, b = ("nope", 1, 2) # would evaluate to False and would not bind a or b
This can clean up a surprising amount of code, especially if you structure a case statement around it.
The double-whammy is when you allow a function to be defined multiple times so long as each definition has a different pattern match in the variable declaration. The Erlang article I linked to covers this topic pretty well.