← ClaudeAtlas

python-patternslisted

Python-specific design patterns and best practices including protocols, dataclasses, context managers, decorators, async/await, type hints, and package organization. Use when working with Python code to apply Pythonic patterns.
SilantevBitcoin/Base-system-Claude · ★ 1 · AI & Automation · score 74
Install: claude install-skill SilantevBitcoin/Base-system-Claude
# Python Patterns > Comprehensive Python patterns extending common design principles with Python-specific idioms. ## Protocol (Duck Typing) Use `Protocol` for structural subtyping (duck typing with type hints): ```python from typing import Protocol class Repository(Protocol): def find_by_id(self, id: str) -> dict | None: ... def save(self, entity: dict) -> dict: ... # Any class with these methods satisfies the protocol class UserRepository: def find_by_id(self, id: str) -> dict | None: # implementation pass def save(self, entity: dict) -> dict: # implementation pass def process_entity(repo: Repository, id: str) -> None: entity = repo.find_by_id(id) # ... process ``` **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: ```python from dataclasses import dataclass, field from typing import Optional @dataclass class CreateUserRequest: name: str email: str age: Optional[int] = None tags: list[str] = field(default_factory=list) @dataclass(frozen=True) class User: """Immutable user entity""" id: str name: str email: str ``` **Features:** - Auto-generated `__init__`, `__repr__`, `__eq__` - `frozen=True` for immutability - `field()` for complex defaults - Type hints for validation ## Context Managers Use context managers (`with` statement) for r