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
caseper 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 it interactively: Format, validate, and auto-document your python directly in JW Solutions Code Formatter & Auto-Doc AI.
Originally published at jw-solutions.com.
Top comments (0)