A simplified explanation of SOLID principles in Python

A simplified explanation of SOLID principles in Python

(OOP programming is a must before diving into it)

As developers, why we should learn SOLID principles?

Well, they help us to create more maintainable, understandable, and flexible software, consequently, open the door to architecture and design patterns.

Let's begin:

First Principle: Single Responsibility

t states that a class should have one responsibility.

Consider a class "Student" with three methods, whose functionalities are registering a student, calculating results, and sending emails. The "Student" class has three responsibilities, which breaks the first principle. To fix it we need to separate each functionality in our program.

No alt text provided for this image

Second Principle: Open/Closed

Consider a "SaveData" class, which has two methods: save to a database and save to a JSON file.

But what if I later want to save the file in an XML file? I must modify the "SaveData" class and consequently, I would be breaking the second principle.

To fix it, we need to design the classes so that when you need to save the data into a different file format, you don’t need to modify it.

No alt text provided for this image

Third principle: Liskov Substitution

It states that any class should be inherent for its parent class without unexpected consequences

So, if it inherits a method that is not useful for the child, the third principle is broken.

Consider an "Animal" parent class with two methods: walk and jump, and then an "Elephant" child class that inherits from "Animal" class.

An elephant can walk without problem, but jump? ...so the jump method does not apply to the class "Elephant".

So, what to do about it? maybe raise an exception? or maybe just put "pass". Unfortunately, no matter what you come up with, the third principle has been broken.

To fix it we need to find a way that it still works with Elephant class, check it out :

No alt text provided for this image

Fourth principle: Interface Segregation


It states that interfaces should be granularly split and be as small as possible.

This is the first principle which is applied on interface, all the above three principles applies on classes.

The key characteristic of interfaces is that they enforce the implementation of certain methods in classes

To do that python provides an abstract base class module called "abc" and "abstractmethod" decorator with which we mark the methods and properties we want to enforce.

Let's see it in action:

No alt text provided for this image

Fifth principle: Dependency Inversion

This principle Suggests

1. High-level modules should not depend on low-level modules. Both should depend on abstractions.

2. Abstractions should not depend on details. Details should depend on abstractions.

Violation of DIP:

No alt text provided for this image

In this example, we found that the high-level module depends on the low-level module. Hence this example are violated the Dependency Inversion Principle. Let’s solve the problem according to the definition of DIP.

No alt text provided for this image

In conclusion, learning SOLID principles will make you a better programmer, and enforce you to write neat, clean code base that is readable by humans.

And that's it, thanks for reading!

To view or add a comment, sign in

More articles by Alexander Garzo

Explore content categories