|

How to Fix: NotImplementedError in Python (OOP Guide)

3D illustration of a robot trying to use a feature that is only a blueprint outline, representing the NotImplementedError.

This error looks scary, but it’s actually a helpful message from a developer. In Python, the NotImplementedError is not a bug; it’s a feature of Object-Oriented Programming (OOP).

It means: “You are supposed to write this method yourself, but you forgot.”

⚡ Quick Fix: NotImplementedError in Python OOP — Abstract Method Override Fix for Child Classes and ABC Abstract Base Class Pattern

A parent class raised NotImplementedError deliberately — it defined a method as a required contract, and your child class never overrode it with a real implementation.

# WRONG — Circle inherits Shape but never defines draw()
class Shape:
    def draw(self):
        raise NotImplementedError("Subclasses must implement draw()")

class Circle(Shape):
    def get_radius(self):
        return 10
    # draw() missing — NotImplementedError fires when called

my_circle = Circle()
my_circle.draw()    # NotImplementedError: Subclasses must implement draw()

# RIGHT — override the method in the child class
class Circle(Shape):
    def get_radius(self):
        return 10

    def draw(self):             # implements the parent's contract
        print("Drawing a circle...")

my_circle = Circle()
my_circle.draw()    # Output: Drawing a circle...

# BETTER — use Python's ABC to catch the missing override at instantiation, not at call time
from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def draw(self):
        pass

class Circle(Shape):
    def draw(self):
        print("Drawing a circle...")

# class Square(Shape): pass  ← ABC blocks instantiation immediately if draw() is missing

The breakdown below explains the raise NotImplementedError pattern versus ABC — and when to use each in a real project.

The Cause: Abstract Methods

Programmers create “abstract” parent classes (blueprints) that are meant to be inherited from. They use NotImplementedError to force the child classes to create their own versions of a method.

Example: The Parent Class (The “Blueprint”)

class Shape:
    def draw(self):
        # I am a blueprint. I don't know how to draw!
        # I force my children to define this.
        raise NotImplementedError("You must implement the 'draw' method!")

The Problem Code (The “Child” Class): You inherit from Shape but forget to define your own .draw() method.

class Circle(Shape):
    def get_radius(self):
        return 10
    
    # Whoops, we forgot to add a 'draw' method!

my_circle = Circle()
my_circle.draw() # CRASH! NotImplementedError

The Fix: Implement the Method

You must “override” the parent’s placeholder method with your own real code.

Correct Code:

class Circle(Shape):
    def get_radius(self):
        return 10
    
    # HERE IS THE FIX:
    def draw(self):
        print("Drawing a circle...")

my_circle = Circle()
my_circle.draw() # Output: Drawing a circle...

If you see this error, Python is telling you that you need to go into your class and write the function that the parent class (or library) is demanding.


NotImplementedError in Python — raise vs ABC, and the Override Pattern That Prevents It

NotImplementedError in Python is a contract violation. A parent class defined a method signature and declared: every child class must implement this. Your child class didn’t.

Two mechanisms enforce this contract. Know which one your codebase uses and which one to build with going forward.

raise NotImplementedError fires at call time. The parent defines the method, raises the error inside it, and the crash happens the first time any code calls that method on an unimplemented child. The error message tells you exactly which method to implement. Add that method to your child class, give it a real body, and the error disappears.

ABC with @abstractmethod fires at instantiation time. Import ABC and abstractmethod from the abc module, inherit from ABC, and decorate each required method with @abstractmethod. Python blocks the child class from being instantiated at all if any abstract method is missing — you see the error the moment you write Circle() rather than when draw() gets called three layers deep in a running application. This is the production-grade pattern for any library, framework, or plugin system where method contracts must be enforced strictly.

from abc import ABC, abstractmethod

class Shape(ABC):
@abstractmethod
def draw(self): pass

@abstractmethod
def area(self): pass    # add as many required methods as the contract demands

class Circle(Shape):
def draw(self):
print(“Drawing a circle…”)

def area(self):
    return 3.14159 * self.radius ** 2

The rule: use raise NotImplementedError for simple single-class blueprints and internal APIs. Use ABC + @abstractmethod for any interface that multiple child classes must implement — Django model mixins, custom data processors, plugin registries, and any architecture where incomplete subclasses should never reach production.

Similar Posts

Leave a Reply