Saltar al contenido
Syntax Tips

PEP 8 in Python 3.12: writing readable match/case blocks

Python's match/case (3.10+) is powerful, but poorly indented it becomes hard to read. PEP 8 recommends indenting each case one level inside match, keeping lines

2026-11-02 · Syntax Tips · PYTHON

Tip of the week

Python's match/case (3.10+) is powerful, but poorly indented it becomes hard to read. PEP 8 recommends indenting each case one level inside match, keeping lines within 79 characters and using short if guards.

match command.split():
    case ["go", direction] if direction in {"north", "south"}:
        move(direction)
    case ["quit" | "exit"]:
        shutdown()
    case _:
        print("Comando desconocido")

Quick rules

  • One case per pattern; avoid guards with more than one condition.
  • Use | to group alternatives instead of repeating branches.
  • Always end with case _: so unexpected input is not silently ignored.

Paste this snippet into the formatter with the button above and check the indentation instantly.

Try snippet in the formatter

View syntax error fix guide: How to fix IndentationError in Python 3.12

Related tips