python-design-patterns — Claude Code skill: Python structure
Use it to shape Python components, separate responsibilities, and decide when repeated code deserves an abstraction. It fits design and refactoring work.
/plugin marketplace add wshobson/agents
/plugin install python-development@claude-code-workflowsWhat python-design-patterns does
The skill applies KISS and single responsibility, recommends clear layers, and favors composition over inheritance. It helps assess coupling, decide whether to introduce an abstraction, and separate business logic from I/O so each layer can be tested on its own.
It includes concise guidance, a formatter registry example, and a pointer to worked examples in references/details.md. Other advice covers constructor dependency injection and keeping composition shallow. The rule of three is a heuristic here: when duplicated code is already diverging and causing bugs, extract the shared behavior sooner.
It is for Python projects and is available for Claude Code, Codex, and Cursor. Install it when you are designing a component or untangling coupled code. It is not aimed at work outside Python.
What it helps with
- split a class with responsibilities from different domains
- decide whether repeated logic needs an abstraction
- choose composition or inheritance for a class hierarchy
- spot dependencies that point from service code into the API layer
- separate business rules from I/O to make tests easier to isolate
Example prompts for the agent
- Review this Python class: what could make it change, and should I split it?
- Should I extract this duplicated logic now or keep both copies?
- Check the imports between the API and service layers and suggest a clean dependency direction.
How to install python-design-patterns
1Plugin marketplace
/plugin marketplace add wshobson/agents/plugin install python-development@claude-code-workflows2Skills CLI (Vercel)
npx skills add wshobson/agents --skill python-design-patterns -a claude-code3Manually: put the files in place
~/.claude/skills/python-design-patterns/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/python-design-patterns/
1Skills CLI (Vercel)
npx skills add wshobson/agents --skill python-design-patterns -a codex2Manually: put the files in place
.agents/skills/python-design-patterns/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add wshobson/agents --skill python-design-patterns -a cursor2Manually: put the files in place
.cursor/skills/python-design-patterns/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install wshobson/agents python-design-patterns2Manually: put the files in place
.github/skills/python-design-patterns/SKILL.mdPlatform CLI
gemini skills install https://github.com/wshobson/agents --path plugins/python-development/skills/python-design-patternsManually: put the files in place
.devin/skills/python-design-patterns/SKILL.mdThe legacy .windsurf/skills/ path still works
Claude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Similar
Questions about python-design-patterns
How do I install python-design-patterns in Claude Code?
Add the marketplace with /plugin marketplace add wshobson/agents, then install with /plugin install python-development@claude-code-workflows. You can also run npx skills add wshobson/agents --skill python-design-patterns -a claude-code.
What does python-design-patterns do?
It guides Python design and refactoring: assigning responsibilities, choosing composition or inheritance, and deciding when an abstraction is warranted.
What if the rule of three says to wait, but duplicated code is already diverging?
The rule of three is a heuristic, not a law. If divergence is causing bugs, the guidance is to extract the shared behavior and add a test.
Source text
SKILL.md · MIT
---
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
## 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
```python
# 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
1. **Keep it simple** - Choose the simplest solution that works
2. **Single responsibility** - Each unit has one reason to change
3. **Separate concerns** - Distinct layers with clear purposes
4. **Compose, don't inherit** - Combine objects for flexibility
5. **Rule of three** - Wait before abstracting
6. **Keep functions small** - 20-50 lines (varies by complexity), one purpose
7. **Inject dependencies** - Constructor injection for testability
8. **Delete before abstracting** - Remove dead code, then consider patterns
9. **Test each layer** - Isolated tests for each concern
10. **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](../python-testing-patterns/SKILL.md) — Test each layer in isolation using the dependency injection structure established here
- [python-project-structure](../python-project-structure/SKILL.md) — Organize modules and directory layout so layer boundaries are explicit from the start
© Seth Hobson · MIT · wshobson/agents