Language Syntax

Pattern class — Python syntax

Check the subject with isinstance and match requested attributes. Positional patterns use __match_args__ where applicable; built-in classes have special single-positional behavior. AttributeError during attribute lookup means a failed match; other exceptions can propagate. Too many positional subpatterns or duplicate keyword names can produce TypeError.

Formal Syntax & Structure

python syntaxpython
case ClassName(positional_pattern, attribute=pattern): suite

Working Example

Example Snippetpython
from dataclasses import dataclass@dataclassclass Point:    x:int    y:intmatch Point(2,3):    case Point(2,y):        result=yprint(result)
Python Reference