Skip to content

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 abcabc. 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 abcabc 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 abcabc module:

interfaces.py
from abc import ABC, abstractmethod
 
class Shape(ABC):
    @abstractmethod
    def area(self):
        pass
 
    @abstractmethod
    def perimeter(self):
        pass
interfaces.py
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 ShapeShape. The ShapeShape interface has two abstract methods areaarea and perimeterperimeter. Any class that implements the ShapeShape interface must provide the implementation for these two methods.

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

rectangle.py
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)
rectangle.py
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 RectangleRectangle that implements the ShapeShape interface. The RectangleRectangle class provides the implementation for the areaarea and perimeterperimeter methods.

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

circle.py
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
circle.py
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 CircleCircle that does not implement the ShapeShape interface. The CircleCircle class provides the implementation for the areaarea and perimeterperimeter methods, but it does not implement the ShapeShape interface.

Interfaces in Python


  classDiagram
    class Shape {
        + area()
        + perimeter()
    }
    class Rectangle {
        - width
        - height
        + area()
        + perimeter()
    }
    class Circle {
        - radius
        + area()
        + perimeter()
    }
    Shape <|-- Rectangle

Interfaces in Python

In this example, we have created an interface called ShapeShape with two abstract methods areaarea and perimeterperimeter. We have created a class called RectangleRectangle that implements the ShapeShape interface. We have also created a class called CircleCircle that does not implement the ShapeShape interface.

Calling Interface Methods

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

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

Output:

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

In this example, we have created an object of the RectangleRectangle class and called the areaarea and perimeterperimeter methods. The RectangleRectangle class implements the ShapeShape interface, so it provides the implementation for the areaarea and perimeterperimeter methods.

You can create an object of the Abstract Base Class, but you cannot call the abstract methods using the object of the Abstract Base Class. You must create a derived class and then call the abstract methods using the object of the derived class.

If you try to call the abstract methods using the object of the Abstract Base Class, you will get an error.

main.py
from interfaces import Shape
 
s = Shape()
print("Area:", s.area())
print("Perimeter:", s.perimeter())
main.py
from interfaces import Shape
 
s = Shape()
print("Area:", s.area())
print("Perimeter:", s.perimeter())

Output:

Error
C:\Users\username\Desktop> python main.py
TypeError: Can't instantiate abstract class Shape with abstract methods area, perimeter
Error
C:\Users\username\Desktop> python main.py
TypeError: Can't instantiate abstract class Shape with abstract methods area, perimeter

In this example, we have created an object of the ShapeShape interface and tried to call the areaarea and perimeterperimeter methods. Since the ShapeShape interface is an abstract class, we cannot create an object of the ShapeShape interface, and we cannot call the areaarea and perimeterperimeter methods using the object of the ShapeShape interface.

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 abcabc module. For more tutorials, you can visit the Python Central Hub.

Was this page helpful?

Let us know how we did