In Python, an interface is a way to define how a class should behave. It’s an agreement that the class will fulfill. However, it is a check for a class to implement all structures otherwise it will return an Error. In Python, you can implement an interface in several ways.
- First, you can use the ABC module of classes that implements the interface.
- Second, you can define a child class that inherits the ABC class as the interface to create an instance of the ABC class.
What is Interface in Python?
First, you need to understand what an interface is. An interface is a set of methods and attributes that provide a common set of functionality to all types that implement it. You can implement an interface by implementing its methods.
However, the interface in Python is implemented with the Abstract base classes module (shorthand ABC). To create an interface in Python, an ABC module is needed to be imported in the Python Script. However, by constructing a subclass you can instantiate the abstract method.
How do ABC Work?
Concrete subclasses inherit abstract classes. Subclasses are forcefully required to implement methods and properties with @abstractmethod and @abstractproperty decorators. However, this way to implement an interface in Python is controlled by the concrete child classes, where an object is created to fulfill the tasks.
Abstract Classes in Python
- The ABC class in the abc module should be the source of every abstract class in Python. Besides constructors and variables, abstract classes may also contain abstract methods, non-abstract methods, and child-classes.
- Subclasses/child classes of abstract classes should implement abstract methods.
- Abstract classes are automatically created if a subclass does not implement the abstract method.
- Abstract class cannot instantiate.
- So by constructing objects for subclasses that inherits abstract classes to access implemented interface for abstract methods.
from abc import ABC, abstractmethod
#Abstract Class
class abstract_class(abc.ABC):
@abstractmethod
def abstract_method(self, a):
pass
class pattern(abstract_class):
def abstract_method(self, a):
print("power: ", (6**a))
i = pattern()
i.abstract_method(2)
power: 36
Concrete class and concrete methods in abstract class to implement an interface
First, make an understanding of what exactly a Concrete class and concrete methods is. Here is the following example that demonstrates in detail how Concrete class and concrete methods work:
- Create an abstract class class_1.
- Consider a concrete method in the class_1 class called concrete_metod() that will just print a string.
- Creating an instance/object is not allowed in Python for abstract class class_1.
- For subclasses of abstract classes, we can create objects to implement an interface for methods, such as class_2_object.
- We can now invoke this method using the object of our concrete class(class_1).
- There is no abstract method @abstractmethod is defined in the following example, so child class (class_2) automatically becomes abstract class.
- Moreover, in the above step we inherit the child class (class_2) to the main class (class_1), then this subclass(class_2) should implement an interface for abstract methods (concrete_metod is the abstract method, detailed demonstrated above).
- Concrete classes use the super() function to invoke the ABC method within the program.
- In the following example, the interface is implemented and prints the message ‘class_1’.
from abc import ABC, abstractmethod, abstractproperty
#ABC is shorthand for Abstract base Class
class class_1(ABC):
def __init__(self, i):
self.i = i
def concrete_metod(self):
print("class_1")
class class_2(class_1):
def __init__(self):
super().__init__("class_2")
#create a subclass object
class_2_object = class_2()
class_2_object.concrete_metod()
class_1
Purpose of Using ABC in Python
- Child classes can implement their own implementations using this capability.
- Maintaining a class list can be challenging when working on complex projects with lots of teams and many classes.
Why Abstract Classes are Crucial to Python?
Abstract classes are crucial to Python for detecting errors if our subclasses fail to follow the coded structure. Taking this approach, we can ensure that all abstract methods defined in the abstract class are implemented by our classes.
An instance or object of an Abstract class in Python
Python does not allow us to create instances or objects of abstract classes. The abstract class raises an error if we attempt to instantiate it.
#import ABC module
from abc import ABC, abstractmethod, abstractproperty
import abc
#Abstract Class
class abstract_class(abc.ABC):
@abstractmethod
def abstract_method(self):
pass
#constructing an object for abstract class
i= abstract_class()
i.abstract_method()
TypeError: Can't instantiate abstract class abstract_class with abstract method abstract_method
resolving the instantiation error To Implement an Interface
Now before resolving this instantiation error first understand why this error is invoked.
- When you manage to create an instance of your abstract class , you will get typeerror ‘cannot instantiate abstract class with abstract methods..’ that means when you construct a function which implements this interface. You should override this function (abstract_method()), the function which we defined as an abstract method.
- Another reason is abstract_childclass_1 is a subclass to abstract_class class, so childeclass (abstract_childclass_1) should provide implementation for abstract method (abstract_method()) of abstract_class class, but it is not providing. So, the subclass abstract_childclass_1 will also be considered as an abstract class. This invokes TypeError.
Solution
It is necessary to provide the abstract_method() method implementation in a class which inherits abstract_class class in order to resolve the typeerror ‘Can’t instantiate abstract class abstract_class with abstract method abstract_method’.
Construct the class again which implements the abstract class instance. Then in the subclass override the class instance from the abstract method. However, in this way you can implement an interface in Python.
#import ABC module
from abc import ABC, abstractmethod, abstractproperty
import abc
#Abstract Class
class abstract_class(abc.ABC):
@abstractmethod
def abstract_method(self):
pass
#childclasses of abstract class abstract_method
class abstract_childclass_1(abstract_class):
def abstract_method(self):
print("Python")
i= abstract_childclass_1()
i.abstract_method()
Python
Conclusion
This article demonstrates how you can implement an interface in Python. Python does not provide room to fully implement a method or interface. However, using the ABC module Python allows us to implement an interface. However, there is a limitation in using it as abstract classes can be as instantiated as objects or instances. So to resolve it a child class that inherits the ABC class is created to create an object for the abstract class and implement its interface.
If you want to learn more about Python Programming, Visit Python Programming Tutorials.