Python Central HubSign in

Python Programming / Python OOPS

Interfaces in Python

Learn about Interfaces in Python. Understand how to create and use interfaces in Python. Python Does not have a native support for interfaces, but we can use Abstract Base Classes to create interfaces in Python.

Interfaces in Python : Implementing Abstract Base Classes

In Python, an interface is a collection of abstract methods. Python does not have a native support for interfaces, but we can use Abstract Base Classes to create interfaces in Python.

An interface is like a contract. It defines the syntax that any class must follow to implement that interface. An interface is like a blueprint for a class. If a class follows the blueprint, it is guaranteed to provide the necessary functionality.

Abstract Base Classes

Python comes with a module which provides the infrastructure for defining Abstract Base Classes (ABCs). The module is called abc. ABCs allow you to define a set of methods that must be implemented by the derived classes.

To create an interface in Python, you can create an abstract class using the abc module. The abstract class can have abstract methods that must be implemented by the derived classes.

Here is an example of an interface in Python using the abc module:

Python
from abc import ABC, abstractmethod
 
class Shape(ABC):
    @abstractmethod
    def area(self):
        pass
 
    @abstractmethod
    def perimeter(self):
        pass

In the above example, we have created an interface called Shape. The Shape interface has two abstract methods area and perimeter. Any class that implements the Shape interface must provide the implementation for these two methods.

Here is an example of a class that implements the Shape interface:

Python
from interfaces import Shape
 
class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height
 
    def area(self):
        return self.width * self.height
 
    def perimeter(self):
        return 2 * (self.width + self.height)

In the above example, we have created a class called Rectangle that implements the Shape interface. The Rectangle class provides the implementation for the area and perimeter methods.

Here is an example of a class that does not implement the Shape interface:

Python
class Circle:
    def __init__(self, radius):
        self.radius = radius
 
    def area(self):
        return 3.14 * self.radius * self.radius
 
    def perimeter(self):
        return 2 * 3.14 * self.radius

In the above example, we have created a class called Circle that does not implement the Shape interface. The Circle class provides the implementation for the area and perimeter methods, but it does not implement the Shape interface.

diagram Interfaces in Python mermaid
Interfaces in Python

In this example, we have created an interface called Shape with two abstract methods area and perimeter. We have created a class called Rectangle that implements the Shape interface. We have also created a class called Circle that does not implement the Shape interface.

Calling Interface Methods

You can call the interface methods using the object of the class that implements the interface. Here is an example:

Python
from rectangle import Rectangle
 
r = Rectangle(5, 10)
print("Area:", r.area())
print("Perimeter:", r.perimeter())

Output:

cmd
C:\Users\username\Desktop> python main.py
Area: 50
Perimeter: 30

In this example, we have created an object of the Rectangle class and called the area and perimeter methods. The Rectangle class implements the Shape interface, so it provides the implementation for the area and perimeter methods.

Conclusion

In Python, an interface is a collection of abstract methods. Python does not have a native support for interfaces, but we can use Abstract Base Classes to create interfaces in Python. An interface is like a contract. It defines the syntax that any class must follow to implement that interface. An interface is like a blueprint for a class. For more information, you can refer to the official documentation of the abc module. For more tutorials, you can visit the Python Central Hub.


Try it: Interfaces (ABC) Exercises

Exercise 1 – Define an Interface

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Multiple Abstract Methods

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – isinstance with Interface

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did