Skills · Coding

Python Design Patterns

Unverified26/40

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.

Originally by wshobson · MIT

Claude CodePartialHas SKILL.md but declares no allowed-tools — Claude Code will ask for permission each time
CursorPartialPlain prose you can paste in — but no Cursor rules file
CodexPartialPlain prose you can paste in — but no AGENTS.md
Gemini CLIPartialPlain prose you can paste in
CopilotPartialPlain prose you can paste in — but no Copilot instructions file
npx agentalley add python-design-patterns

This command does not work yet — the CLI is still being built. Until then, use Raw in the reader below to take the file.

Who is stuck, and on what

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.

The whole source

No sign-in, no blur, nothing truncated
python-design-patterns/SKILL.md86 lines4.6 KBRawView on GitHub
Frontmatter — 2 properties
namepython-design-patterns
descriptionPython 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.
1---
2name: python-design-patterns
3description: 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.B1Line is 663 characters — unreadable by eye
4---A5No allowed-tools declared — no way to tell what this skill may touch
5 
6# Python Design Patterns
7 
8Write 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 
23Choose the simplest solution that works. Complexity must be justified by concrete requirements.
24 
25### 2. Single Responsibility (SRP)
26 
27Each unit should have one reason to change. Separate concerns into focused components.
28 
29### 3. Composition Over Inheritance
30 
31Build behavior by combining objects, not extending classes.
32 
33### 4. Rule of Three
34 
35Wait until you have three instances before abstracting. Duplication is often better than premature abstraction.
36 
37## Quick Start
38 
39```python
40# Simple beats clever
41# Instead of a factory/registry pattern:
42FORMATTERS = {"json": JsonFormatter, "csv": CsvFormatter}
43 
44def get_formatter(name: str) -> Formatter:
45 return FORMATTERS[name]()
46```
47 
48## Detailed patterns and worked examples
49 
50Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
51 
52## Best Practices Summary
53 
541. **Keep it simple** - Choose the simplest solution that works
552. **Single responsibility** - Each unit has one reason to change
563. **Separate concerns** - Distinct layers with clear purposes
574. **Compose, don't inherit** - Combine objects for flexibility
585. **Rule of three** - Wait before abstracting
596. **Keep functions small** - 20-50 lines (varies by complexity), one purpose
607. **Inject dependencies** - Constructor injection for testability
618. **Delete before abstracting** - Remove dead code, then consider patterns
629. **Test each layer** - Isolated tests for each concern
6310. **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.**
68Apply 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.**
71This 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.**
74Keep 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.**
77Duplication 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.**
80This 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](../python-testing-patterns/SKILL.md) — Test each layer in isolation using the dependency injection structure established here
85- [python-project-structure](../python-project-structure/SKILL.md) — Organize modules and directory layout so layer boundaries are explicit from the start
86 

Reviews

Installed this one?Write the first review and take the Trailblazer badge.

Reviews only open after a real install, so this is empty — and we leave it empty rather than invent one.

Alternatives

Also in Coding