Python design patterns including KISS

Python design patterns including KISS, Separation of Concerns, Single Responsibility, and composition over inheritance. Use this skill when designing a new service or component from scratch and choosing how to layer responsibilities,…
--- name: python-design-patterns description: Python design patterns including KISS, Separation of Concerns, Single Responsibility, and composition over inheritance. Use this skill when designing a new service or component from scratch and choosing how to layer responsibilities, when refactoring a God class or monolithic function that has grown too large, when deciding whether to add a new abstraction or live with duplication, when evaluating a pull request for structural issues like tight coupling or leaking internal types, when choosing between inheritance and composition for a new class hierarchy, or when a codebase is becoming hard to test because of entangled I/O and business logic. --- # 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
Overview
Write maintainable Python code using fundamental design principles. These patterns help you build systems that are easy to understand, test, and modify.
Manual install (npx, ZIP)
npx skills add wshobson/agents --skill python-design-patterns -g -a claude-codemkdir -p ~/.claude/skills && curl -sL https://codeload.github.com/wshobson/agents/tar.gz/4236bb91f839 \
| tar -xz -C ~/.claude/skills --strip-components=4 agents-4236bb91f839/plugins/python-development/skills/python-design-patternsNew-Item -ItemType Directory -Force "$HOME\.claude\skills" | Out-Null; Invoke-WebRequest "https://m.agentalley.io/zip/b025d9845b104da554e6/python-design-patterns.zip" -OutFile "$env:TEMP\python-design-patterns.zip"; Expand-Archive "$env:TEMP\python-design-patterns.zip" -DestinationPath "$HOME\.claude\skills" -ForceConnects with Agent Alley MCP · agentalley.io/mcp
Files included
SKILL.md4.6 KBreferences/1 file · 10.7 KB
Read the files here
wshobson/
Shared references2SKILL.md reads these from elsewhere in the repo. Not in the ZIP.
- SKILL.mdplugins/python-development/skills/python-project-structure/6.6 KB
- SKILL.mdplugins/python-development/skills/python-testing-patterns/7.1 KB
Show the full text86 lines
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
| 1 | |
| 2 | name python-design-patterns |
| 3 | description Python design patterns including KISS, Separation of Concerns, Single Responsibility, and composition over inheritance. Use this skill when designing a new service or component from scratch and choosing how to layer responsibilities, when refactoring a God class or monolithic function that has grown too large, when deciding whether to add a new abstraction or live with duplication, when evaluating a pull request for structural issues like tight coupling or leaking internal types, when choosing between inheritance and composition for a new class hierarchy, or when a codebase is becoming hard to test because of entangled I/O and business logic. |
| 4 | |
| 5 | |
| 6 | # Python Design Patterns |
| 7 | |
| 8 | Write maintainable Python code using fundamental design principles. These patterns help you build systems that are easy to understand, test, and modify. |
| 9 | |
| 10 | ## When to Use This Skill |
| 11 | |
| 12 | Designing new components or services |
| 13 | Refactoring complex or tangled code |
| 14 | Deciding whether to create an abstraction |
| 15 | Choosing between inheritance and composition |
| 16 | Evaluating code complexity and coupling |
| 17 | Planning modular architectures |
| 18 | |
| 19 | ## Core Concepts |
| 20 | |
| 21 | ### 1. KISS (Keep It Simple) |
| 22 | |
| 23 | Choose the simplest solution that works. Complexity must be justified by concrete requirements. |
| 24 | |
| 25 | ### 2. Single Responsibility (SRP) |
| 26 | |
| 27 | Each unit should have one reason to change. Separate concerns into focused components. |
| 28 | |
| 29 | ### 3. Composition Over Inheritance |
| 30 | |
| 31 | Build behavior by combining objects, not extending classes. |
| 32 | |
| 33 | ### 4. Rule of Three |
| 34 | |
| 35 | Wait until you have three instances before abstracting. Duplication is often better than premature abstraction. |
| 36 | |
| 37 | ## Quick Start |
| 38 | |
| 39 | |
| 40 | # Simple beats clever |
| 41 | # Instead of a factory/registry pattern: |
| 42 | FORMATTERS = {"json": JsonFormatter, "csv": CsvFormatter} |
| 43 | |
| 44 | def get_formatter(name: str) -> Formatter: |
| 45 | return FORMATTERS[name]() |
| 46 | |
| 47 | |
| 48 | ## Detailed patterns and worked examples |
| 49 | |
| 50 | Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient. |
| 51 | |
| 52 | ## Best Practices Summary |
| 53 | |
| 54 | **Keep it simple** - Choose the simplest solution that works |
| 55 | **Single responsibility** - Each unit has one reason to change |
| 56 | **Separate concerns** - Distinct layers with clear purposes |
| 57 | **Compose, don't inherit** - Combine objects for flexibility |
| 58 | **Rule of three** - Wait before abstracting |
| 59 | **Keep functions small** - 20-50 lines (varies by complexity), one purpose |
| 60 | **Inject dependencies** - Constructor injection for testability |
| 61 | **Delete before abstracting** - Remove dead code, then consider patterns |
| 62 | **Test each layer** - Isolated tests for each concern |
| 63 | **Explicit over clever** - Readable code beats elegant code |
| 64 | |
| 65 | ## Troubleshooting |
| 66 | |
| 67 | **A class is growing and seems to have multiple responsibilities, but splitting it feels wrong.** |
| 68 | 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. |
| 69 | |
| 70 | **Injecting all dependencies through the constructor is producing constructors with 7+ parameters.** |
| 71 | 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. |
| 72 | |
| 73 | **Composition is producing deeply nested wrapper objects that are hard to trace.** |
| 74 | 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. |
| 75 | |
| 76 | **The rule of three says not to abstract yet, but the duplication is causing bugs when one copy is updated but not the other.** |
| 77 | 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. |
| 78 | |
| 79 | **A service layer is importing from the API layer, breaking the dependency direction.** |
| 80 | 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). |
| 81 | |
| 82 | ## Related Skills |
| 83 | |
| 84 | [python-testing-patterns] — Test each layer in isolation using the dependency injection structure established here |
| 85 | [python-project-structure] — Organize modules and directory layout so layer boundaries are explicit from the start |
| 86 |
Details
- Updated
- Sep 21, 2026
- License
- MIT
- Stars on the repo
- ★ 39,857
- Checked
- Source
- wshobson/agents ↗
- Creator
- wshobson · 179 listings
Similar skills
See all →
SkillExtract text, tables, metadataXberg Document Extraction
SkillMove code from one implementation to another functionCode migration
SkillManages Ahrefs API usage in Python using ahrefs-python
MCPDocx LLM translatoradeu
SkillCreate an Amazon-compliant white-background main imageAmazon White-Background Main Image · 亚马逊白底主图
MCPOfficial monorepo of SDKs, CLISendmux sdkMore from wshobson
SkillMaster C/.NET backend development patterns for building
SkillImplement WCAG 2.2 compliant interfaces with mobileAccessibility compliance
SkillMaster Material Design 3 and Jetpack Compose patternsAndroid Mobile Design
SkillMigrate from AngularJS to Angular using hybrid modeAngular migration
SkillUnderstand anti-reversing, obfuscationAnti reversing techniques
SkillBuild production Apache Airflow DAGs with best practicesApache Airflow DAG Patterns