Python Patterns
This skill provides comprehensive Python patterns extending common design principles with Python-specific idioms.
Protocol (Duck Typing)
Use Protocol for structural subtyping (duck typing with type hints):
Benefits:
- Type safety without inheritance
- Flexible, loosely coupled code
- Easy testing and mocking
Dataclasses as DTOs
Use dataclass for data transfer objects and value objects:
Features:
- Auto-generated
__init__,__repr__,__eq__ frozen=Truefor immutabilityfield()for complex defaults- Type hints for validation
Context Managers
Use context managers (with statement) for resource management:
Class-based context manager:
Generators
Use generators for lazy evaluation and memory-efficient iteration:
Generator expressions:
Decorators
Function Decorators
Class Decorators
Async/Await
Async Functions
Async Context Managers
Type Hints
Advanced Type Hints
Union Types (Python 3.10+)
Dependency Injection
Constructor Injection
Package Organization
Project Structure
Module Exports
Error Handling
Custom Exceptions
Exception Groups (Python 3.11+)
Property Decorators
Functional Programming
Higher-Order Functions
When to Use This Skill
- Designing Python APIs and packages
- Implementing async/concurrent systems
- Structuring Python projects
- Writing Pythonic code
- Refactoring Python codebases
- Type-safe Python development


