← Back to Error Guide
NotImplementedError

How to Fix NotImplementedError

Raised to indicate that an abstract method or operation has not been implemented.

What is NotImplementedError?

The NotImplementedError is a built-in exception in Python that is typically raised when an abstract method or required functionality in a base class or interface has not been implemented by a subclass. It serves as a reminder for developers to provide the necessary method implementations in subclasses, especially when designing frameworks or reusable code.

Common Causes

How to Fix

  1. Identify the method or operation that raises NotImplementedError.
  2. Provide a concrete implementation for that method in the subclass or relevant class.

Wrong Code

# Wrong code that causes error
class Animal:
    def speak(self):
        raise NotImplementedError('Subclasses must implement this method')

class Dog(Animal):
    pass

d = Dog()
d.speak()  # Raises NotImplementedError

Correct Code

# Correct code
class Animal:
    def speak(self):
        raise NotImplementedError('Subclasses must implement this method')

class Dog(Animal):
    def speak(self):
        return 'Woof!'

d = Dog()
print(d.speak())  # Outputs: Woof!

More Python Error Guides