Abstract methods in python

Abstract methods are methods that are declared in a class but do not have an implementation. They are meant to be overridden by subclasses. In Python, abstract methods are defined using the @abstractmethod decorator and the ABC (Abstract Base Class) module.

Solution 1: Using Abstract Base Classes

The first solution involves using the ABC module and the @abstractmethod decorator to define abstract methods in Python.


from abc import ABC, abstractmethod

class AbstractClass(ABC):
    @abstractmethod
    def abstract_method(self):
        pass

class ConcreteClass(AbstractClass):
    def abstract_method(self):
        print("Implementation of abstract method")

# Creating an instance of the ConcreteClass
obj = ConcreteClass()
obj.abstract_method()

In this solution, we define an abstract method abstract_method in the AbstractClass using the @abstractmethod decorator. The ConcreteClass inherits from the AbstractClass and provides an implementation for the abstract method. Finally, we create an instance of the ConcreteClass and call the abstract method.

Solution 2: Using ABCMeta Metaclass

The second solution involves using the ABCMeta metaclass and the @abstractmethod decorator to define abstract methods in Python.


from abc import ABCMeta, abstractmethod

class AbstractClass(metaclass=ABCMeta):
    @abstractmethod
    def abstract_method(self):
        pass

class ConcreteClass(AbstractClass):
    def abstract_method(self):
        print("Implementation of abstract method")

# Creating an instance of the ConcreteClass
obj = ConcreteClass()
obj.abstract_method()

In this solution, we define an abstract method abstract_method in the AbstractClass using the @abstractmethod decorator and set the metaclass to ABCMeta. The ConcreteClass inherits from the AbstractClass and provides an implementation for the abstract method. Finally, we create an instance of the ConcreteClass and call the abstract method.

Solution 3: Using ABCMeta Metaclass and abstractmethod as a Function

The third solution involves using the ABCMeta metaclass and the abstractmethod function to define abstract methods in Python.


from abc import ABCMeta, abstractmethod

class AbstractClass(metaclass=ABCMeta):
    def abstract_method(self):
        raise NotImplementedError

class ConcreteClass(AbstractClass):
    def abstract_method(self):
        print("Implementation of abstract method")

# Creating an instance of the ConcreteClass
obj = ConcreteClass()
obj.abstract_method()

In this solution, we define an abstract method abstract_method in the AbstractClass by raising a NotImplementedError. The ConcreteClass inherits from the AbstractClass and provides an implementation for the abstract method. Finally, we create an instance of the ConcreteClass and call the abstract method.

Among the three options, Solution 1 using Abstract Base Classes is the recommended approach for defining abstract methods in Python. It provides a clear and explicit way to define abstract methods and enforce their implementation in subclasses. Solution 2 and Solution 3 also work, but they are less commonly used and may be less intuitive for other developers.

Rate this post

7 Responses

    1. Solution 3 may appear extensive, but it offers the advantage of being more robust and maintainable in the long run. Sometimes, a little extra effort can save you from headaches later on. Its about striving for quality, my friend.

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents