I would like to create a match-case statement in Python in which two cases do the same instruction. For instance, if Python behaved like C++, according to this Stack Overflow question, I would write the match-case statement as:
match x:
case 1:
case 2:
# Some code
break
case 3:
# Some code
break
The code however does not work, showing that the match-case statement needs to be written in a different way in Python. What is this way?
match x:
case 1 | 2:
# Some code
case 3:
# Some code
Python match clauses don't fall through, so you can't concatenate them as in C and you don't use break at the end of the clause.
The "OR pattern" (as shown, where | means "or") can be used with subpatterns which bind variables, but all the subpatterns need to bind the same variables.