In Python match-case, a guard is an if condition added after a pattern (case Point(x=0, y=y) if y > 0:), and a class pattern is ClassName(...) that checks the type of the subject and then matches its attributes. Together they let you branch on both the shape and the values of an object in one readable statement. Both need Python 3.10 or newer, and everything below runs on Python 3.13 and 3.14.
A case matches only when its pattern matches and, if there is a guard, the guard evaluates to true. If the guard is false, Python moves on to the next case, and any names the pattern bound stay assigned.
__match_args__A class pattern looks like a constructor call, but it never creates an object. It runs an isinstance() check and then compares attributes.
Point() with no arguments only checks the type. A keyword such as x=0 compares the attribute p.x with a literal, while y=y binds the attribute to a new local name y. Subclasses of Point match too, because the check is isinstance.
__match_args__You can also write Point(0, y). Positional sub-patterns are translated to attribute names through the class attribute __match_args__, a tuple of attribute names.
Dataclasses and named tuples define __match_args__ for you, in field order. A plain class does not, so positional patterns raise a TypeError until you add it yourself:
Built-in types such as int, str, float, list and dict accept a single positional sub-pattern that matches the whole value, so case int(n): binds n to the integer.
A guard runs only after the pattern has matched, so it can use the names the pattern just bound.
Use a guard for anything a pattern cannot express: comparisons, ranges, calls to helper functions, or relations between two captured values (as with x == y above, which a pattern cannot say because repeating a capture name in one pattern is an error).
Cases are tried top to bottom, so put specific cases and guarded cases before the general ones. If a pattern matches but its guard fails, the captured names remain bound afterwards. Do not rely on a name being unset after a failed guard, and do not reuse capture names for unrelated purposes in the same function.
A guard that raises an exception propagates it; it is not treated as false. Keep guards free of side effects, because a guard may run even though its case is not chosen.
Class patterns nest with sequence, mapping, or-patterns and as captures.
The or-pattern Circle() | Rect() as s matches either class and binds the subject to s. In an or-pattern every alternative must bind the same set of names.
case Point: is a capture pattern that matches anything and binds the name Point; you need parentheses, case Point():. Python reports an error if such an irrefutable case is not the last one.Color.RED.__match_args__, which raise TypeError.case _: before more specific cases. Later cases become unreachable.Yes. The guard runs after the pattern matches, so every name the pattern bound is available, as in case Point(x=x, y=y) if x == y:.
Python simply tries the next case. The names the pattern captured stay assigned, but the case body does not run.
No. Point(x=0) in a case only tests isinstance(subject, Point) and then reads attributes. Nothing is constructed or modified.
Define __match_args__ as a tuple of attribute names, or make the class a dataclass or named tuple, which does it for you.
A class pattern checks the type of the subject and matches its attributes by keyword or, through __match_args__, by position. A guard adds an if condition that is evaluated only after the pattern matches and can use the captured names. Put specific and guarded cases first, use Class() with parentheses for type-only checks, and keep guards free of side effects.