The S.O.L.I.D Principles in Python
At some point in your development journey, you might have heard of the design principles — S.O.L.I.D during your work. There is no shortage of resources and examples on the web regarding these 5 principles, and in this post, I would like to just document and contribute my own understanding of these principles in the Python language. I will also share some references which I found very useful to help me better understand these principles at the end.
S — Single responsibility principle
A class should have only one reason to change
My Understanding:
Each class or module should have one responsibility, thus one reason to change the code. If you find that you need to change a class or module for multiple reasons, it could likely be a violation of this principle.
class Car:
def __init__(self, car_name):
self.car_name = car_name def get_car_name(self):
pass
def add_car_to_garage(self, garage):
pass
The above code is a violation. Aside from the main get_car_name method, if you change how the garage store the car (eg: changing data structure from list to tuples/dictionary), you need to update this Car class as well. The Car class is having additional responsibilities that…