python-design-patterns

Applies Python design principles like SRP, composition, and dependency injection to structure maintainable code.

3|1|Updated Nov 30, 2025
One-click install
npx skills add https://github.com/PALabs-v1/AI_friend --skill python-design-patterns-palabs-v1
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-design-patterns
Source: https://github.com/PALabs-v1/AI_friend/tree/main/.agents/skills/python-design-patterns
Command: npx skills add https://github.com/PALabs-v1/AI_friend --skill python-design-patterns-palabs-v1

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Python codebases often grow into tangled God classes, monolithic functions, and tightly coupled layers that are hard to test and modify. This Skill provides concrete design patterns and refactoring guidance to keep code simple, modular, and testable. ## Core Features & Use Cases - Fundamental Patterns: Apply KISS, Single Responsibility, Separation of Concerns, and composition over inheritance with before-and-after code examples. - Structural Guidance: Organize code into API, service, and repository layers with correct dependency direction, plus constructor-based dependency injection for testability. - Refactoring Heuristics: Use the Rule of Three, function size guidelines, and anti-pattern detection (leaking ORM types, mixing I/O with business logic) when reviewing or restructuring code. - Use Case: When a pull request introduces a handler that parses HTTP, validates input, queries the database, and formats the response all in one class, use this Skill to split it into handler, service, and repository layers with injected dependencies. ## Quick Start Review my UserHandler class using the python-design-patterns skill and refactor it into separate handler, service, and repository layers.

Frequently Asked Questions about python-design-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I refactor a God class in Python?

Apply the Single Responsibility Principle by listing every reason the class could change. Split concerns from different domains, such as HTTP parsing, business rules, and data access, into separate handler, service, and repository classes with injected dependencies.

When should I use composition instead of inheritance in Python?

Prefer composition when building behavior from interchangeable parts, such as notification channels or formatters. Passing collaborators through the constructor makes classes flexible and easy to test with fakes, while inheritance creates rigid hierarchies that are hard to mock.

How do I structure a Python service into layers?

Organize code into three layers: handlers for HTTP parsing and responses, services for business logic, and repositories for data access. Dependencies must point downward only, so services never import from the API layer.

When should I avoid creating an abstraction in Python?

Follow the Rule of Three: wait until you have three similar instances before abstracting. Two similar-looking functions often hide different validation and error handling, so duplication is usually better than a premature or wrong abstraction.

Why does constructor injection produce too many parameters?

Constructors with seven or more parameters signal that the class has too many responsibilities, not that dependency injection is wrong. Split the class into smaller focused units first, and each constructor naturally becomes smaller.