Featurecore
OR, AS and group patterns
Also called or pattern, as pattern, group pattern, case with vertical bar, case a or b, bind a subpattern with as, parentheses in patterns.
P1 | P2 matches when one of its alternatives matches. P as name matches P and binds the whole subject to name. A group pattern, (P), only adds parentheses to make the grouping clear.
- Language
- Python
Synopsis
def status(code):
match code:
case 200 | 201 | 204:
return "ok"
case (401 | 403 | 404) as err:
return f"not allowed ({err})"
case _:
return "other"
print(status(201), status(403), status(500))
ok not allowed (403) other
| joins alternatives. as err binds the subject to err when the OR pattern matches.
Semantics
r1. "An OR pattern matches each of its subpatterns in turn to the subject value, until one succeeds. The OR pattern is then considered successful. Otherwise, if none of the subpatterns succeed, the OR pattern fails."1 Example: or-as.
r2. In an OR pattern, "Only the final subpattern may be irrefutable, and each subpattern must bind the same set of names to avoid ambiguity."1 Examples: same-names, different-names.
r3. "If the OR pattern fails, the AS pattern fails. Otherwise, the AS pattern binds the subject to the name on the right of the as keyword and succeeds." Also, the "capture_pattern cannot be a _."1 Example: as-subpattern.
r4. "A group pattern allows users to add parentheses around patterns to emphasize the intended grouping." "In simple terms (P) has the same effect as P."1
r5. The tutorial shows as on a subpattern: case (Point(x1, y1), Point(x2, y2) as p2): ... "will capture the second element of the input as p2 (as long as the input is a sequence of two points)".2 Example: as-subpattern.
Examples
def move(command):
match command:
case ["go", ("north" | "south" | "east" | "west") as direction]:
return f"going {direction}"
case ["go", other]:
return f"cannot go {other}"
print(move(["go", "east"]))
print(move(["go", "up"]))
going east
cannot go up
def first_or_only(value):
match value:
case [x, *_] | x:
return x
print(first_or_only([7, 8]), first_or_only(9))
7 9
Both alternatives bind x, and only the last one is irrefutable (rule r2).
Pitfalls
The alternatives of an OR pattern must bind the same names:
This code raises SyntaxError, on purpose.
match (1, 2):
case (x, 0) | (0, y):
print(x)
Bind the same names in each alternative, or use two case blocks.
In case 401 | 403 as err, the as applies to the whole OR pattern, because the grammar is or_pattern 'as' capture_pattern.1 Parentheses, as in the synopsis, make this clear (rule r4).
| in a pattern is not the or operator. case 1 or 2: is a syntax error:
This code raises SyntaxError, on purpose.
match 2:
case 1 or 2:
print("one or two")
Comparison
A Go case can list several expressions. In an expression switch, "the case expressions, which need not be constants, are evaluated left-to-right and top-to-bottom; the first one that equals the switch expression triggers execution of the statements of the associated case".3 So case 200, 201, 204: in Go is like case 200 | 201 | 204: in Python. A Go expression switch has no AS form, because its cases are expressions that Go compares with the switch expression, and they bind no names.
References
- The Python Language Reference, Python 3.15, 8.6.4.1. OR Patterns; 8.6.4.2. AS Patterns; 8.6.4.7. Group Patterns
- The Python Tutorial, 4. More Control Flow Tools, 4.7. match Statements
- The Go Programming Language Specification, Expression switches
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag 27YVT5 Edit this page History Report an error Markdown