New to Claude Skills? Learn how to install them →

Pwshobson on GitHub

Python Design Patterns

Free

Design maintainable Python code with core principles.

by wshobson38.7k stars on wshobson/agents
2 views
Updated Jul 18, 2026
Get this skill

Free · Opens the source repo

What Python Design Patterns does

The Python Design Patterns skill provides developers with a framework for writing maintainable and scalable Python applications. By adhering to fundamental design principles such as KISS (Keep It Simple), Single Responsibility, and Composition Over Inheritance, this skill guides users in structuring their code effectively. It is particularly beneficial when starting new projects or refactoring existing codebases that have become complex or unwieldy. With a focus on clear responsibilities and modular architecture, this skill helps ensure that systems are easy to understand, test, and modify.

When designing new components or services, this skill offers practical advice on how to layer responsibilities and avoid common pitfalls. For instance, it emphasizes the importance of waiting until a pattern has been repeated three times before abstracting it, which helps prevent premature optimization and unnecessary complexity. Additionally, the skill provides guidance on evaluating code for structural issues, such as tight coupling and the misuse of inheritance, allowing developers to maintain clean and efficient codebases.

The skill also includes troubleshooting tips for common design challenges, such as managing class responsibilities and dependency injection. By addressing these issues with concrete strategies, users can improve the quality of their code and enhance its testability. Overall, the Python Design Patterns skill is an essential resource for developers looking to implement best practices in their Python projects, making it a valuable addition to any developer's toolkit.

When to use it

Use this skill when designing new services or components, or when refactoring existing code to improve maintainability.

When not to use it

This skill may not be suitable for very small scripts or projects where design patterns add unnecessary complexity.

What you can build with it

Designing a New Service

When starting a new project, use this skill to apply design patterns that ensure a clean architecture from the outset.

Refactoring a God Class

If you have a class that has grown too large and complex, leverage the principles in this skill to break it down into smaller, more manageable components.

Evaluating Code for Coupling Issues

Use this skill to assess pull requests for structural issues, ensuring that your code remains loosely coupled and maintainable.

How to install Python Design Patterns

View source

1. Install with the skills CLI

npx skills add wshobson/agents/python-design-patterns --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by wshobson

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

Frequently asked questions about Python Design Patterns

Similar skills