TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
match statement, introduced in Python 3.10 via PEP 634, implements structural pattern matching. It evaluates a subject expression against a series of case blocks containing structural patterns, executing the block associated with the first successful match. Unlike traditional C-style switch statements, it performs data destructuring, variable binding, and type checking simultaneously.
Both match and case are soft keywords. This language semantic means they function as reserved keywords only within the specific syntax of a pattern matching block. Developers can still use match and case as variable, class, or function names elsewhere in their code without raising a SyntaxError.
Core Components
- Subject: The expression evaluated once at the entry point of the
matchblock. - Pattern: The structural definition following the
casekeyword. Patterns are evaluated top-to-bottom. - Guard: An optional
ifclause appended to acase. If the structural pattern matches, the guard expression is evaluated. If the guard evaluates toFalse, the match fails, and evaluation proceeds to the nextcase. - Wildcard (
_): An irrefutable pattern that matches any subject without binding it to a variable. It is typically used as the finalcaseto act as a default fallback.
Pattern Types and Mechanics
Literal Patterns Matches the subject against constant values (e.g., integers, strings, booleans,None). Comparison is performed using equality (==), except for singletons like None, True, and False, which are compared using identity (is).
Enum.VALUE, module.CONSTANT, or cls.CONSTANT). Attempting to match a constant using a simple, non-dotted name (e.g., case MY_CONSTANT:) is a critical pitfall: Python will interpret it as a Capture Pattern, silently matching any value, shadowing the intended constant, and introducing logical bugs.
collections.abc.Sequence (e.g., list, tuple, but explicitly excluding str, bytes, and bytearray). It supports exact length matching and variable-length unpacking using the * operator.
collections.abc.Mapping (e.g., dict). It checks for the presence of specified keys and matches their corresponding values. Extra keys in the subject do not cause the match to fail. The ** operator can capture remaining key-value pairs.
__match_args__ tuple to map positions to attribute names.
|)
Combines multiple patterns into a single case. The match succeeds if any of the sub-patterns match. If capture variables are used, all sub-patterns must bind the exact same variables; otherwise, Python enforces this at compile time and will raise a SyntaxError (SyntaxError: alternative patterns bind different names).
as)
Binds a successfully matched pattern (or sub-pattern) to a specific variable name. This allows for capturing a value while simultaneously enforcing a structural or type constraint.
Master Python with Deep Grasping Methodology!Learn More





