Python Design Patterns
Seth HobsonCommunityCreated by Seth Hobson
- Skill
- Guidelines
- Software development
- MIT
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a framework for building clean, testable, and modular Python systems. It emphasizes practical design principles over complex abstractions, helping developers decide when to refactor, how to layer responsibilities, and when to favor composition over inheritance. By focusing on clear boundaries and the 'rule of three,' it helps prevent over-engineering while ensuring code remains flexible as requirements evolve.
What it does
- Defines core principles like KISS, Single Responsibility, and composition over inheritance.
- Provides strategies for refactoring monolithic functions and God classes.
- Offers guidance on layering architectures to separate HTTP, business logic, and data access.
- Explains how to use dependency injection to improve testability.
- Provides heuristics for when to introduce abstractions versus living with duplication.
- Includes troubleshooting advice for common structural and dependency issues.
When to use it
- When designing new services or components from scratch.
- When refactoring complex, tangled, or monolithic codebases.
- When evaluating pull requests for structural issues like tight coupling.
- When deciding between inheritance and composition for class hierarchies.
- When code is becoming difficult to test due to entangled I/O and business logic.
Advantages
- Promotes maintainable and readable code through explicit design.
- Reduces over-engineering by encouraging simple, pragmatic solutions.
- Improves testability by decoupling business logic from infrastructure.
- Provides clear rules for managing dependencies and layer boundaries.
- Helps developers identify and resolve structural code smells.
Usage examples
- “How should I structure a new user registration feature to keep HTTP logic separate from database operations?”
- The skill advises using a layered approach: an API handler for request parsing, a service layer for business rules, and a repository layer for database access.
- “I have a class that is getting too large and handles both validation and database saving. What should I do?”
- The skill suggests applying the Single Responsibility Principle by splitting the class based on 'reasons to change,' separating domain concerns from data access.
Install
Download the skill as a ZIP:
https://mp-staging.autogineer.com/agent-skills/wshobson/python-design-patterns/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip python-design-patterns.zip -d .agents/skills
Documentation by Seth Hobson
Python Design Patterns
Write maintainable Python code using fundamental design principles. These patterns help you build systems that are easy to understand, test, and modify.
When to Use This Skill
Designing new components or services
Refactoring complex or tangled code
Deciding whether to create an abstraction
Choosing between inheritance and composition
Evaluating code complexity and coupling
Planning modular architectures
Core Concepts
1. KISS (Keep It Simple)
Choose the simplest solution that works. Complexity must be justified by concrete requirements.
2. Single Responsibility (SRP)
Each unit should have one reason to change. Separate concerns into focused components.
3. Composition Over Inheritance
Build behavior by combining objects, not extending classes.
4. Rule of Three
Wait until you have three instances before abstracting. Duplication is often better than premature abstraction.
Quick Start
# Simple beats clever
# Instead of a factory/registry pattern:
FORMATTERS = {"json": JsonFormatter, "csv": CsvFormatter}
def get_formatter(name: str) -> Formatter:
return FORMATTERS[name]()Detailed patterns and worked examples
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.
Best Practices Summary
Keep it simple - Choose the simplest solution that works
Single responsibility - Each unit has one reason to change
Separate concerns - Distinct layers with clear purposes
Compose, don't inherit - Combine objects for flexibility
Rule of three - Wait before abstracting
Keep functions small - 20-50 lines (varies by complexity), one purpose
Inject dependencies - Constructor injection for testability
Delete before abstracting - Remove dead code, then consider patterns
Test each layer - Isolated tests for each concern
Explicit over clever - Readable code beats elegant code
Troubleshooting
A class is growing and seems to have multiple responsibilities, but splitting it feels wrong. Apply the "reason to change" test: list every change that could require editing this class. If the list has items from different domains (e.g., HTTP parsing AND business rules AND formatting), split it. If all changes stem from the same domain concern, the class may be appropriately sized.
Injecting all dependencies through the constructor is producing constructors with 7+ parameters. This is a sign of too many responsibilities in one class, not a problem with dependency injection. Split the class into smaller units first, then each constructor naturally becomes smaller.
Composition is producing deeply nested wrapper objects that are hard to trace. Keep the composition shallow (2-3 levels). If wrapping is the only mechanism, consider whether a Protocol-based approach or simple function composition would be cleaner than a chain of decorator objects.
The rule of three says not to abstract yet, but the duplication is causing bugs when one copy is updated but not the other. Duplication that diverges in dangerous ways should be abstracted sooner. The rule of three is a heuristic, not a law. If the copies are already diverging incorrectly, extract immediately and add a test that exercises the shared behavior.
A service layer is importing from the API layer, breaking the dependency direction. This is a layering violation. The service layer must not import from handlers. Introduce a shared types/models layer that both can import from, keeping the dependency arrow pointing downward (API → Service → Repository).
Related Skills
python-testing-patterns — Test each layer in isolation using the dependency injection structure established here
python-project-structure — Organize modules and directory layout so layer boundaries are explicit from the start
Files
- ATTRIBUTION.mdAdded by AutoGineer1002 B
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md4.6 KiB
- references/details.md10.7 KiB
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Needs Python
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- Seth Hobson
- Publisher
- Seth Hobson (Community)
- Source
- wshobson/agents · plugins/python-development/skills/python-design-patterns · commit 4bde316
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Seth Hobson.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download python-design-patterns.zip