Programming Search⌘K

Featurecore

Literal, capture and wildcard patterns

Also called literal pattern, capture pattern, wildcard pattern, value pattern, case underscore, match a constant in a case, dotted name pattern.

A literal pattern matches an equal value. A capture pattern, a bare name, matches anything and binds it. The wildcard _ matches anything and binds nothing. A dotted name is a value pattern.

Language
Python

Synopsis

def http_error(status):
    match status:
        case 400:
            return "Bad request"
        case 404:
            return "Not found"
        case 418:
            return "I'm a teapot"
        case _:
            return "Something's wrong with the internet"

print(http_error(404))
print(http_error(500))
Output
Not found
Something's wrong with the internet

This is the example of the tutorial. 400, 404 and 418 are literal patterns, and _ is the wildcard.

Semantics

r1. Literal patterns: "In simple terms, LITERAL will succeed only if == LITERAL. For the singletons None, True and False, the is operator is used."1 Example: singletons.

r2. "Triple-quoted strings are supported. Raw strings and byte strings are supported. f-strings and t-strings are not supported."1

r3. "A capture pattern binds the subject value to a name." "Capture patterns always succeed."1 The name "becomes a local variable in the closest containing function scope unless there’s an applicable global or nonlocal statement."1 Example: capture.

r4. "In a given pattern, a given name can only be bound once."1 Example: same-name.

r5. "A wildcard pattern always succeeds (matches anything) and binds no name." "_ is a soft keyword within any pattern, but only within patterns."1 Example: http-error.

r6. A value pattern is a dotted name. "The dotted name in the pattern is looked up using standard Python name resolution rules. The pattern succeeds if the value found compares equal to the subject value (using the == equality operator)."1 Named constants "must be dotted names to prevent them from being interpreted as capture variables".2 Example: dotted.

Examples

def kind(value):
    match value:
        case True:
            return "True"
        case 1:
            return "one"
        case None:
            return "None"
        case _:
            return "other"

print(kind(True), kind(1), kind(1.0), kind(None), kind(0))
Output
True one one None other

case True uses is, so 1 does not match it. case 1 uses ==, so 1.0 matches it (rule r1).

def describe(command):
    match command:
        case "quit":
            return "bye"
        case other:
            return f"unknown command {other!r}"

print(describe("quit"))
print(describe("jump"))
Output
bye
unknown command 'jump'
from enum import Enum

class Color(Enum):
    RED = "red"
    GREEN = "green"

def show(color):
    match color:
        case Color.RED:
            return "I see red!"
        case Color.GREEN:
            return "Grass is green"

print(show(Color("green")))
Output
Grass is green

Pitfalls

A bare name is a capture pattern, not a comparison with a variable:

This code raises SyntaxError, on purpose.

RED = "red"

match "blue":
    case RED:
        print("red")
    case _:
        print("other")

case RED captures each value, so it is irrefutable. The reference says that "A match statement may have at most one irrefutable case block, and it must be last."1 So Python rejects the program. Use a dotted name, such as Color.RED, or a guard such as case c if c == RED (rule r6).

A name cannot be bound twice in one pattern:

This code raises SyntaxError, on purpose.

match (1, 1):
    case (x, x):
        print("same")

Use case (x, y) if x == y to test that two items are equal.

Comparison

A Go expression switch compares values: "the first one that equals the switch expression triggers execution of the statements of the associated case; the other cases are skipped."3 The default case of Go is like the wildcard _ (rule r5). In Go, "the case expressions, which need not be constants", are evaluated, so a Go case can name a variable to compare with it.3 In Python, a bare name in a case captures the value, and only a dotted name compares (rule r6).

References

  1. The Python Language Reference, Python 3.15, 8.6.4.3. Literal Patterns; 8.6.4.4. Capture Patterns; 8.6.4.5. Wildcard Patterns; 8.6.4.6. Value Patterns
  2. The Python Tutorial, 4. More Control Flow Tools, 4.7. match Statements
  3. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source