Open–closed principle

This principle state "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"
It means that when you think about the architecture of your program, you have to think about a way to make it easy for any developer to add different functionality by just adding code without having to modify (remove or modify) the existing code.

Code example

Bad implementation
  
    from dataclasses import dataclass
    from abc import abstractmethod


    # Bad implementation
    class Dog:
        size: str

        def bark(self):
            if self.size == "big":
                print("WOF WOF")
                return

            if self.size == "small":
                print("wof wof")
                return

            if self.size == "medium":
                print("wof WOF")
                return
  
In the previous implementation, if the user want to add a medium dog, he has to modify an existing method. In the following version, the user doesn't have to modify existing code. Like you can see here, a good way to implement the the open-close principle is to use abstract interface.
Good Implementation
  

    # Good implementation
    class Dog:
    
        @abstractmethod
        def bark(self):
            pass
    
    
    class BigDog(Dog):
    
        def bark(self):
            print("WOF WOF")
    
    
    class SmallDog(Dog):
    
        def bark(self):
            print("wof wof")
    
    
    # If the user want to add a medium dog, it is easy
    
    
    class MediumDog(Dog):
    
        def bark(self):
            print("wof WOF")
    

  

References

  • https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle
  • "Clean Architecture: A Craftsman's Guide to Software Structure and Design" by Robert Martin