How to Solve NotImplementedError in Python

To solve NotImplementedError in Python, use the abstract method in every child class. The NotImplementedError is raised on Runtime, and it is a user-generated exception.

NotImplementedError in Python

NotImplementedError exception is mainly used in abstract classes. An abstract class is created, and inside the abstract class, in which this error is raised. If the program does not override the method in the subclass, it will display this error message. The implement is the keyword for inheriting subclass from the Parent class. 

Abstract methods should be overridden in the sub-class. Not implemented means that this error message will be thrown to the user if an abstract method is not implemented in the base class. 

See the following code example.

class Animal:
  def __init__(self, name):
   self.name = name

 def eat(self):
   raise NotImplementedError(
    "You must need to create the eat method in every child class")


class Dog(Animal):
  def __int__(self, name, legs):
    super().__init__(name)
    self.legs = legs


class Cow(Animal):
  def __init__(self, name, color):
    super().__init__(name)
    self.color = color


# Create an object for the child class
c1 = Cow("Milky", "Brown")
c1.eat()

Output

NotImplementedError: You must need to create the eat method in every child class

In this example, we created a parent class known as the Animal. The animal class consists of the constructor and a method named eat().

There are two child classes, one is Dog and the other is Cow. Both of these classes are inherited from the Animal class. Inside the Dog class, we have created a constructor which calls the parent class constructor.

We are assigning names and legs to the Dog class.

Inside the Cow class, we have created a constructor inside which we are calling the parent class constructor using the super keyword. We are assigning names and colors for the cow class. 

And then we have created an object for the Cow class with the name Milky and the color Brown.  When we create this object the Cow class constructor is instantiated and assigns the value for name and color. In the last line, we are calling the eat method.

When this is executed an error occurs. The error message is displayed as: You must need to create the eat() method in every child class. 

We are calling the eat() method that is for the Cow class but the method in the Animal class is executed. This is because we don’t have a eat method inside the Cow class. The method in the parent class should be created inside the child class. This method is called the abstract method. This error is a user-raised error. 

An abstract method is a type of method in which there will be a separate copy of that method inside the child class. This can be overridden inside the child class.

To solve this problem, we have to create the eat() method inside the parent class method and put the eat() method inside all the child classes.

class Animal:
  def __init__(self, name):
    self.name = name

  def eat(self):
   raise NotImplementedError(
    "You must need to create the eat method in every child class")


class Dog(Animal):
  def __init__(self, name, legs):
    super().__init__(name)
    self.legs = legs

  def eat(self):
    print("Dog likes Pedigree !!")


class Cow(Animal):
  def __init__(self, name, color):
    super().__init__(name)
    self.color = color

  def eat(self):
    print("Cow likes green leaves")


# Create an object for the child class
cow = Cow("Milky", "Brown")
cow.eat()
dog = Dog("Tommy", 4)
dog.eat()

Output

Cow likes green leaves
Dog likes Pedigree !!

Conclusion

The NotImplementedError is raised when you do not implement the abstract method in the child class. This error can be solved by using the abstract method in every child class. If we use the abstract method inside the child class, a new instance of that method is created inside the child class. This concept is known as abstraction.

That’s it for this tutorial.

See also

How to Solve OverflowError in Python

How to Solve TypeError: ‘str’ object is not callable in Python

How to Solve FloatingPointError in Python

How to Solve EOFError in Python

How to Solve TypeError: must be str, not int in Python

Leave a Comment