Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A literal pattern is a structural pattern matching construct in Python that evaluates whether a subject expression exactly matches a specified constant literal. Operating within a match...case block, it acts as the simplest form of pattern matching, routing execution flow based on direct value equivalence.

Evaluation Mechanics

The underlying comparison mechanism depends on the type of literal provided in the case clause:
  1. Equality (==): For numbers (integers, floats, complex), strings, and bytes, Python evaluates the pattern using the equality operator. The match succeeds if subject == literal.
  2. Identity (is): For singletons (None, True, and False), Python strictly evaluates the pattern using the identity operator. The match succeeds only if subject is literal.

Syntax

match subject_expression:
    case "exact_string":
        # Executes if subject_expression == "exact_string"
    case 42:
        # Executes if subject_expression == 42
    case 3.14:
        # Executes if subject_expression == 3.14
    case True:
        # Executes if subject_expression is True
    case None:
        # Executes if subject_expression is None

Supported Literal Types

Literal patterns strictly accept the following built-in constant types:
  • Strings: "text", 'text', r"raw"
  • Bytes: b"bytes"
  • Integers: 10, -5, 0xFF
  • Floats: 3.14, 1e10
  • Complex Numbers: 3+4j
  • Booleans: True, False
  • Null Object: None

Technical Constraints and Nuances

1. F-Strings are Invalid Formatted string literals (f-strings) cannot be used as literal patterns. Because f-strings are evaluated dynamically at runtime, they violate the requirement that patterns must be static constants.

# SyntaxError: patterns may not contain f-strings
case f"prefix_{value}": 
2. Bare Names Create Capture Patterns A common developer pitfall is attempting to use a local variable as a literal pattern. A bare, unquoted identifier in a case statement is interpreted by the parser as a Capture Pattern, which unconditionally binds the subject to that variable name rather than comparing their values.
MY_CONST = 100

match subject:
    case MY_CONST: 
        # This does NOT check if subject == 100.
        # It captures the subject's value into a new local variable named 'MY_CONST',
        # shadowing the outer variable.
3. Dotted Names (Value Patterns) To match against a predefined constant without triggering a capture pattern, you must use a dotted name (an attribute lookup). In the Python grammar, this is technically classified as a Value Pattern, but it serves the exact same mechanical purpose as a literal pattern.
import enum

class Status(enum.Enum):
    SUCCESS = 1

match subject:
    case Status.SUCCESS:
        # Evaluates using equality: subject == Status.SUCCESS
4. Float Precision Because floating-point literal patterns rely on ==, they are subject to standard IEEE 754 precision limitations. A subject resulting from a mathematical operation (e.g., 0.1 + 0.2) will fail to match a literal pattern of case 0.3: due to floating-point representation errors.
Master Python with Deep Grasping Methodology!Learn More