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
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
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
abc
module:
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
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
Shape
. The Shape
Shape
interface has two abstract methods area
area
and perimeter
perimeter
. Any class that implements the Shape
Shape
interface must provide the implementation for these two methods.
Here is an example of a class that implements the Shape
Shape
interface:
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)
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
Rectangle
that implements the Shape
Shape
interface. The Rectangle
Rectangle
class provides the implementation for the area
area
and perimeter
perimeter
methods.
Here is an example of a class that does not implement the Shape
Shape
interface:
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
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
Circle
that does not implement the Shape
Shape
interface. The Circle
Circle
class provides the implementation for the area
area
and perimeter
perimeter
methods, but it does not implement the Shape
Shape
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 Shape
Shape
with two abstract methods area
area
and perimeter
perimeter
. We have created a class called Rectangle
Rectangle
that implements the Shape
Shape
interface. We have also created a class called Circle
Circle
that does not implement the Shape
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:
from rectangle import Rectangle
r = Rectangle(5, 10)
print("Area:", r.area())
print("Perimeter:", r.perimeter())
from rectangle import Rectangle
r = Rectangle(5, 10)
print("Area:", r.area())
print("Perimeter:", r.perimeter())
Output:
C:\Users\username\Desktop> python main.py
Area: 50
Perimeter: 30
C:\Users\username\Desktop> python main.py
Area: 50
Perimeter: 30
In this example, we have created an object of the Rectangle
Rectangle
class and called the area
area
and perimeter
perimeter
methods. The Rectangle
Rectangle
class implements the Shape
Shape
interface, so it provides the implementation for the area
area
and perimeter
perimeter
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.
from interfaces import Shape
s = Shape()
print("Area:", s.area())
print("Perimeter:", s.perimeter())
from interfaces import Shape
s = Shape()
print("Area:", s.area())
print("Perimeter:", s.perimeter())
Output:
C:\Users\username\Desktop> python main.py
TypeError: Can't instantiate abstract class Shape with abstract methods area, perimeter
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 Shape
Shape
interface and tried to call the area
area
and perimeter
perimeter
methods. Since the Shape
Shape
interface is an abstract class, we cannot create an object of the Shape
Shape
interface, and we cannot call the area
area
and perimeter
perimeter
methods using the object of the Shape
Shape
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 abc
abc
module. For more tutorials, you can visit the Python Central Hub.
Was this page helpful?
Let us know how we did