Skip to main content
Idiomatic Python patterns and best practices for building robust, efficient, and maintainable applications.

When to Activate

  • Writing new Python code
  • Reviewing Python code
  • Refactoring existing Python code
  • Designing Python packages/modules

Core Principles

1. Readability Counts

2. EAFP - Easier to Ask Forgiveness Than Permission

Type Hints

Modern Type Hints (Python 3.9+)

Protocol-Based Duck Typing

Error Handling

Specific Exception Handling

Custom Exception Hierarchy

Data Classes

Data Classes with Validation

Decorators

Function Decorators

Parameterized Decorators

Async/Await for Concurrent I/O

Quick Reference: Python Idioms

Anti-Patterns to Avoid

Bad:
Good:
Bad:
Good:
Bad:
Good:
Python code should be readable, explicit, and follow the principle of least surprise. When in doubt, prioritize clarity over cleverness.