Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Keyword match

Evaluate the subject once and try case patterns in order.

Version: Python 3.14; repository image, production runtime not attested

Syntax

match subject:
    case pattern:
        ...

Evaluate the subject once and try case patterns in order.

The first matching pattern with a true or absent guard selects a suite, with no fall-through.

No match simply leaves the statement when there is no wildcard arm. match and case are soft keywords and remain valid identifiers in other contexts.

Examples

def sample(value):
    calls = []
    def subject():
        calls.append("once")
        return value
    match subject():
        case [x, y]:
            calls.append(x + y)
        case {"total": amount}:
            calls.append(amount)
    return calls
print(sample([2, 3]), sample({"total": 7}), sample("ab"))

Result

['once', 5] ['once', 7] ['once']

No match simply leaves the statement when there is no wildcard arm. match and case are soft keywords and remain valid identifiers in other contexts.

Sources