Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
wshobson avatar

Python Design Patterns

  • 17.4k installs
  • 38.3k repo stars
  • Updated July 22, 2026
  • wshobson/agents

Python Design Patterns is a guide for writing maintainable Python using fundamental design principles and architectural patterns.

About

Write maintainable Python code using design principles like KISS, Single Responsibility, and composition over inheritance. Use when designing new services, refactoring complex code, or evaluating code structure for coupling and testability.

  • Fundamental design principles: KISS, Single Responsibility, and composition over inheritance
  • Patterns for refactoring complex code and deciding when to abstract
  • Guidelines for layering, dependency injection, and testability

Python Design Patterns by the numbers

  • 17,350 all-time installs (skills.sh)
  • +564 installs in the week ending Jul 28, 2026 (Skillselion tracking)
  • Ranked #21 of 1,382 Code Review & Quality skills by installs in the Skillselion catalog
  • Security screen: LOW risk (skills.sh audit)
  • Data as of Jul 28, 2026 (Skillselion catalog sync)
At a glance

python-design-patterns capabilities & compatibility

Use cases
refactoring · code review
Runs
Runs locally
Pricing
Free
npx skills add https://github.com/wshobson/agents --skill python-design-patterns

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs17.4k
repo stars38.3k
Security audit3 / 3 scanners passed
Last updatedJuly 22, 2026
Repositorywshobson/agents

How do you refactor a Python God class cleanly?

Design and refactor Python systems using composition, dependency injection, and single responsibility principles.

Who is it for?

Refactoring God classes, deciding between inheritance and composition, designing modular systems, evaluating code quality

Skip if: Framework-specific patterns (use framework docs)

When should I use this skill?

User designs a new Python service, splits a God class, debates inheritance vs composition, or reports untestable entangled I/O logic.

What you get

Layered Python modules with separated concerns, injected dependencies, and composition-based class design.

  • refactored module structure
  • composition-based class designs
  • layer boundary guidance

By the numbers

  • Target function size: 20-50 lines with one purpose
  • Rule of Three: wait for three instances before abstracting

Files

SKILL.mdMarkdownGitHub ↗

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

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 — Test each layer in isolation using the dependency injection structure established here
  • python-project-setup — Set up project structure and tooling that enforces layer boundaries from the start

Related skills

How it compares

Pick python-design-patterns for Python structural design; use python-testing-patterns from the same repo when the primary need is test harness setup.

FAQ

What is the Rule of Three?

Wait until you have three instances of duplication before abstracting; duplication is often better than premature abstraction.

How do I apply Single Responsibility?

A class should have only one reason to change. If changes stem from different domains, split the class.

Is Python Design Patterns safe to install?

skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.