Open/Closed Principle in Python: Extend Code Without Breaking What Works
The Open/Closed Principle (OCP) is a software design guideline stating that code should be open for extension but closed for modification. A common anti-pattern involves repeatedly adding elif blocks to existing functions whenever new requirements arrive, which increases bug risk and regression testing overhead. Python supports OCP through duck typing, first-class functions, and decorator-based registries, often without requiring traditional inheritance. For example, a decorator registry allows new export formats to be added simply by defining and registering a new function, leaving the core dispatcher untouched. These approaches form the basis of plugin-style architectures and help keep growing codebases maintainable and stable.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in