3 ms·
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 follo
by ketzu 6y ago
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.
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_overflow_users_rejoice_as_pattern_matching/gmu03eh/ https://www.reddit.com/r/programming/comments/lgqhmj/stack_o...
[2] https://www.python.org/dev/peps/pep-0635/ https://www.python.org/dev/peps/pep-0635/