Aller au contenu
Skillpedia
Programmation générale41k · #6760

python-design-patterns

Ce qu’il fait

Ce skill vous aide à organiser votre code Python pour qu'il soit plus facile à comprendre, à tester et à modifier. Il vous enseigne des principes comme garder les choses simples et donner à chaque morceau de code une seule responsabilité.

Quand l’utiliser

  • ✓Au moment de commencer un nouveau projet ou une partie de projet.
  • ✓Quand le code existant est trop désordonné et difficile à modifier.
  • ✓Quand vous devez décider s'il faut créer une nouvelle structure ou répéter du code.

Lien du skill

https://github.com/wshobson/agents/blob/main/plugins/python-development/skills/python-design-patterns/SKILL.md

Copiez-le et collez-le dans votre IA (Claude, ChatGPT ou Cursor) en lui demandant de suivre les instructions.

Comment l’utiliser

Le plus simple : copiez le message et collez-le dans votre IA. Elle lit le skill et suit les instructions.

Message prêt à coller

Utilise ce skill pour m’aider : https://github.com/wshobson/agents/blob/main/plugins/python-development/skills/python-design-patterns/SKILL.md Lis le fichier SKILL.md de ce lien et suis ses instructions pour ma tâche.
Envie que votre IA trouve seule des skills comme celui-ci ?Installez le MCP gratuit de Skillpedia : une ligne, et elle cherche parmi 15 000+ skills avant chaque tâche.Installer en 1 minute →

Exemple de demande à votre IA

“Comment puis-je organiser mon code Python pour le rendre plus facile à maintenir ?”

5 nouveaux skills par semaine. 1 minute de lecture.

Comment ça marche

Le skill suggère de suivre des principes comme « garder les choses simples » et « chaque partie avec une seule responsabilité ». Il vous apprend aussi à combiner des objets plutôt qu'à créer des hiérarchies complexes, et à attendre d'avoir trois exemples avant de créer une nouvelle structure.

Contenu technique (SKILL.md original)

Texte original de l’auteur, en anglais, directement depuis GitHub.

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

Questions fréquentes

Que signifie « composition plutôt qu'héritage » ?

Cela signifie qu'il vaut mieux construire des fonctionnalités en combinant des objets existants, plutôt que de créer de nouvelles classes qui héritent d'anciennes.

Quand dois-je créer une nouvelle abstraction ?

La règle générale est d'attendre d'avoir trois exemples similaires. Avant cela, répéter un peu de code est souvent plus simple et plus sûr que de créer une abstraction trop tôt.

Mon code devient trop gros et désordonné. Que dois-je faire ?

Découpez le code en parties plus petites, dont chacune n'a qu'une seule raison d'être modifiée. Chaque partie sera ainsi plus ciblée et plus facile à gérer.

Détails

Description originale de l’auteur

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

repo: wshobson/agents

Popularité

Étoiles GitHub
41k
Position au classement
#6760
Licence
MIT
Mis à jour
15/10/2026

Ce skill vous est-il utile ?

0

Connectez-vous pour voter.

Commentaires

  • …

Autres skills de ce dépôt

Skills similaires