Interface Segregation Principle in Python


The Interface Segregation Principle (ISP) is a fundamental principle of object-oriented design that promotes the use of small, focused interfaces to increase code reusability and maintainability. In this blog, we will discuss the Interface Segregation Principle and provide an example in Python.
Understanding the Interface Segregation Principle
The Interface Segregation Principle is a part of the SOLID principles of object-oriented programming. It states that a client should not be forced to depend on interfaces it does not use. In other words, classes should be designed with small, focused interfaces that are tailored to the needs of their clients.
The Interface Segregation Principle can be summarized in the following statement: “Clients should not be forced to depend on interfaces they do not use.” This means that a class should only implement the methods that are relevant to its clients, and not implement methods that are not needed.
Example of the Interface Segregation Principle in Python
Let’s consider an example to understand the Interface Segregation Principle in Python. Suppose we have a class name Document
that represents a document. The Document
class has a method called print
, which prints the document. We also have a class named Scanner
that represents a scanner. The Scanner
class has a method called scan
, which scans a document.
Now, suppose we have a class named MultifunctionalDevice
that represents a multifunctional device, which is capable of printing and scanning documents. We can implement the MultifunctionalDevice
class by inheriting from both the Document
and Scanner
classes. However, this approach violates the Interface Segregation Principle since it forces clients of the MultifunctionalDevice
class to depend on both print
and scan
methods, even if they only need one of them.
Here is an example of how we can implement the Document
, Scanner
, and MultifunctionalDevice
classes in Python while adhering to the Interface Segregation Principle:
class Printable:
def print(self):
pass
class Scanner:
def scan(self):
pass
class Document(Printable):
def print(self):
print("Printing document...")
class MultifunctionalDevice(Printable, Scanner):
def print(self):
print("Printing document...")
def scan(self):
print("Scanning document...")
In this example, we have created two interfaces: Printable
and Scanner
. The Printable
interface defines a print
method, and the Scanner
interface defines a scan
method. We have also created a class named Document
that implements the Printable
interface and a class named MultifunctionalDevice
that implements both the Printable
and Scanner
interfaces.
By using this approach, we can ensure that clients of the MultifunctionalDevice
class are only forced to depend on the methods they need, and not on methods they do not need. This makes the code more flexible and maintainable.
Conclusion
In conclusion, the Interface Segregation Principle is a fundamental principle of object-oriented design that promotes code reusability and maintainability. By following this principle, we can create software systems that are easy to extend and maintain. In the example provided above, we demonstrated how to implement the Interface Segregation Principle in Python using a simple example. By following the Interface Segregation Principle, we can create software systems that are more flexible and maintainable.
Keep reading and follow me and my team to stay updated on the latest tech world blogs.
To read more about best practices:
SOLID2 :Open-Closed Principle in Python
Subscribe to my newsletter
Read articles from NonStop io Technologies directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

NonStop io Technologies
NonStop io Technologies
Product Development as an Expertise Since 2015 Founded in August 2015, we are a USA-based Bespoke Engineering Studio providing Product Development as an Expertise. With 80+ satisfied clients worldwide, we serve startups and enterprises across San Francisco, Seattle, New York, London, Pune, Bangalore, Tokyo and other prominent technology hubs.