Python NotImplementedError – Tutorial with Examples

Python NotImplementedError - Tutorial with Examples

In this tutorial, we will discuss the Python NotImplementedError in detail. We will see what the Python NotImplementedError is, how to use it, when it should be used, and some examples to understand it better.

Introduction

The NotImplementedError is a built-in exception in Python that indicates that the child class has not implemented the method that is inherited from its parent class. It is raised when we try to call an abstract method that is not implemented in the derived class.

The NotImplementedError is a way to ensure that the child classes implement the required methods from the parent class. It indicates that there is more work to be done to get the required functionality, and reminds the developer to complete the implementation of the abstract method.

Using the NotImplementedError

When we define an abstract method in the parent class, it is expected that the child classes will implement this method in their own way. However, if a child class does not implement the abstract method, then we can raise a NotImplementedError to indicate that the implementation is missing.

We can raise the NotImplementedError using the following code:

class Parent:
    def abstract_method(self):
        raise NotImplementedError("Implement this method in child class")

In the above code, we have defined an abstract_method() in the parent class. This method raises a NotImplementedError with a message to remind the developer to implement this method in the child class. Remember, if we want to use raise NotImplementedError in Python, we must pass an argument to it, which can be a string or a message indicating that the implementation is missing.

When to use NotImplementedError

We use NotImplementedError in Python when we define an abstract method in the parent class that must be implemented in the child classes. If we do not provide the implementation of the abstract method in the child classes, we can raise a NotImplementedError to indicate that there is work to be done. It reminds the developer to complete the implementation of the abstract method.

The other way we can use NotImplementedError is when we are working with a module or a library that requires us to implement specific methods in it. If there is a method that has not been implemented, we can raise a NotImplementedError to indicate that the implementation is missing.

Examples to Understand NotImplementedError Better

Example 1: Using NotImplementedError with Abstract Methods

Let’s create an example to understand how to use the NotImplementedError with abstract methods.

class Shape:
    def area(self):
        raise NotImplementedError("Provide the implementation of the area method")
 
class Square(Shape):
    def __init__(self, length):
        self.length = length
    
    def area(self):
        return self.length * self.length

s = Square(4)
print("Area of square: ", s.area())

# Attempt to create an instance of the Shape class.
try:
    s = Shape()
    print("Area of shape: ", s.area())
except Exception as e:
    print(e)

In the above code, we have defined a Shape class with an abstract method called area(). The area method raises a NotImplementedError to indicate that it needs to be implemented in the derived class. We then define a Square class that inherits from the Shape class and provides a concrete implementation of the area() method.

In the above code, we have also tried to create an instance of Shape class which is abstract and should not be instantiated. That is why we caught exception and showed it as output.

The output of the above code will be:

Area of square:  16
Can't instantiate abstract class Shape with abstract methods area

As we can see from the output, we are able to create an instance of the Square class and calculate the area of the square. But when we try to instantiate the Shape class, a NotImplementedError is raised with the message indicating that the area method needs to be implemented in the derived class.

Example 2: Using NotImplementedError with Module

Let’s create an example to understand how to use the NotImplementedError with a module.

Suppose we have a module called operations.py that defines a function called divide().

# operations.py
 
def divide(a, b):
    raise NotImplementedError("Provide the implementation of divide method")

In the above code, we have defined a divide() function that raises a NotImplementedError with a message indicating that the implementation of this function is missing.

We can use this module in another Python file and implement the divide() function.

# main.py
import operations
 
def divide(a, b):
    return a / b
 
print(divide(10, 2))

In the above code, we have imported the operations module and defined our own divide() function. We have provided the implementation of the divide() function.

The output of the above code will be:

5.0

As we can see from the output, when we call the divide() function in the main module, it returns the result without raising any NotImplementedError.

Example 3: Raise NotImplementedError with Derived Class

Let’s create an example to understand how to raise the NotImplementedError with the derived class.

class Vehicle:
    def vehicleType(self):
        raise NotImplementedError("Provide the implementation of vehicleType method")
 
class Car(Vehicle):
    def __init__(self):
        pass

c = Car()
c.vehicleType()

In the above code, we have defined a class called Vehicle with an abstract method called vehicleType(). The vehicleType() method raises a NotImplementedError to indicate that the implementation needs to be added in the derived class. We then define a Car class that inherits from the Vehicle class.

In the above code, we have created an instance of Car class and tried to call the vehicleType() method, which is supposed to be implemented in the derived class, but we have not provided the implementation yet.

The output of the above code will be:

NotImplementedError: Provide the implementation of vehicleType method

As we can see from the output, when we call the vehicleType() method on the Car object, it raises a NotImplementedError with the message indicating that the implementation needs to be added to the derived class.

Conclusion

In this tutorial, we learned about the Python NotImplementedError. We saw what the NotImplementedError is, how to use it, when it should be used, and some examples to understand it better. Using the NotImplementedError allows us to ensure that the child classes have implemented the required methods from the parent class.

Leave a Reply

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