From 9efe6c2447a3767187d40c725e4d1065600b47e5 Mon Sep 17 00:00:00 2001 From: Abhiranjan Kumar <2400030581@kluniversity.in> Date: Mon, 3 Nov 2025 12:53:22 +0530 Subject: [PATCH 1/3] added default return value in get_localizer to prevent KeyError --- patterns/creational/factory.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/patterns/creational/factory.py b/patterns/creational/factory.py index e5372ca5..f75bb2b2 100644 --- a/patterns/creational/factory.py +++ b/patterns/creational/factory.py @@ -54,7 +54,8 @@ def get_localizer(language: str = "English") -> Localizer: "Greek": GreekLocalizer, } - return localizers[language]() + return localizers.get(language, EnglishLocalizer)() + def main(): From 8c63fd0d8971c5ee9960a6a634f32348fb1f20d7 Mon Sep 17 00:00:00 2001 From: Abhiranjan Kumar <2400030581@kluniversity.in> Date: Mon, 3 Nov 2025 12:58:07 +0530 Subject: [PATCH 2/3] cleaned up docstring formatting and removed unnecessary asterisks --- patterns/creational/builder.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/patterns/creational/builder.py b/patterns/creational/builder.py index 22383923..16af2295 100644 --- a/patterns/creational/builder.py +++ b/patterns/creational/builder.py @@ -1,13 +1,12 @@ """ -*What is this pattern about? +What is this pattern about? It decouples the creation of a complex object and its representation, so that the same process can be reused to build objects from the same family. This is useful when you must separate the specification of an object from its actual representation (generally for abstraction). -*What does this example do? - +What does this example do? The first example achieves this by using an abstract base class for a building, where the initializer (__init__ method) specifies the steps needed, and the concrete subclasses implement these steps. @@ -22,16 +21,15 @@ class for a building, where the initializer (__init__ method) specifies the In general, in Python this won't be necessary, but a second example showing this kind of arrangement is also included. -*Where is the pattern used practically? - -*References: -https://sourcemaking.com/design_patterns/builder +Where is the pattern used practically? +See: https://sourcemaking.com/design_patterns/builder -*TL;DR +TL;DR Decouples the creation of a complex object and its representation. """ + # Abstract Building class Building: def __init__(self) -> None: From f44385e8a6060dbce50fa14f2e2583ea7c905a48 Mon Sep 17 00:00:00 2001 From: ABHIRANJAN-KUMAR1 Date: Fri, 7 Nov 2025 16:59:53 +0530 Subject: [PATCH 3/3] Add Anti-Patterns section explaining discouraged design patterns --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 05222bc9..2379d39a 100644 --- a/README.md +++ b/README.md @@ -119,3 +119,27 @@ You can also run `flake8` or `pytest` commands manually. Examples can be found i ## Contributing via issue triage [![Open Source Helpers](https://www.codetriage.com/faif/python-patterns/badges/users.svg)](https://www.codetriage.com/faif/python-patterns) You can triage issues and pull requests which may include reproducing bug reports or asking for vital information, such as version numbers or reproduction instructions. If you would like to start triaging issues, one easy way to get started is to [subscribe to python-patterns on CodeTriage](https://www.codetriage.com/faif/python-patterns). + + +## 🚫 Anti-Patterns + +This section lists some common design patterns that are **not recommended** in Python and explains why. + +### 🧱 Singleton +**Why not:** +- Python modules are already singletons β€” every module is imported only once. +- Explicit singleton classes add unnecessary complexity. +- Better alternatives: use module-level variables or dependency injection. + +### πŸŒ€ God Object +**Why not:** +- Centralizes too much logic in a single class. +- Makes code harder to test and maintain. +- Better alternative: split functionality into smaller, cohesive classes. + +### πŸ” Inheritance overuse +**Why not:** +- Deep inheritance trees make code brittle. +- Prefer composition and delegation. +- β€œFavor composition over inheritance.” +