Language Syntax
Pattern guard — Python syntax
Evaluate the guard only after the pattern succeeds and its captures are available. A falsy guard moves on to the next case; an exception from the guard propagates. Captures may remain bound even if the guard fails. Do not depend on bindings from a failed pattern; partial binding behavior is deliberately unspecified.
Formal Syntax & Structure
python syntaxpython
case pattern if condition: suiteParameters & Attributes
| Name | Type | Description |
|---|---|---|
| pattern | pattern | Must succeed before guard evaluation. |
| condition | expression | Truth test using available captures. |
Working Example
Example Snippetpython
def label(v): match v: case int(n) if n>0: return 'positive' case int(): return 'nonpositive' case _: return 'other'print((label(2),label(-1),label('2')))