NotImplementedError

The base class says "a subclass must provide this" — you get the error only when the missing method is actually called, not when the object is created.

InheritsBaseException›Exception›RuntimeError›NotImplementedError
Runtime exceptionPython 3 (all)Live demo
NotImplementedError(*args)
Raised by
raise NotImplementedError in a base-class method a subclass did not override
Message
whatever you pass — often empty
Quick fix
implement the method in the subclass
Watch out
NotImplemented (constant) is for operators; NotImplementedError is for methods

Demo

Live evaluation
Pick an exporter. JsonExporter inherits export() from the base class without overriding it.
Try:
Inputs
fmtstrcsv, json, …
Code
class Exporter:
    def export(self, data):
        raise NotImplementedError('subclasses must implement export()')
class CsvExporter(Exporter):
    def export(self, data):
        return ','.join(data)
class JsonExporter(Exporter):
    pass
exporters = {'csv': CsvExporter(), 'json': JsonExporter()}
exporters['csv'].export(['a', 'b'])
Result
'a,b'

Creating JsonExporter() worked fine — the error only appears when export() is called, because the base method runs and raises. Compare the unknown format: that is a KeyError from the dict, a different problem entirely. abc.abstractmethod (see the pitfalls) moves the failure earlier, to object creation.

Constructor

NameTypeRequiredDescription
*argsobjectnoOptional message, e.g. which method must be overridden. With no args the traceback line is the bare class name.

Attributes

AttributeTypeMeaning
argstupleThe message you passed, or () for a bare raise NotImplementedError.

Common patterns

Base-class hook
The classic use: a method that subclasses must provide. Name it in the message.
class Shape:
    def area(self):
        raise NotImplementedError(f'{type(self).__name__} must implement area()')
Enforced at creation with abc
An ABC refuses to instantiate a subclass that forgot an abstract method — the bug shows up at construction, not deep in a call.
from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self): ...
Binary operator: return NotImplemented
For __eq__, __add__, __lt__ and friends, return the NotImplemented constant so Python can try the other operand — never raise NotImplementedError there.
class Money:
    def __init__(self, cents):
        self.cents = cents
    def __add__(self, other):
        if not isinstance(other, Money):
            return NotImplemented
        return Money(self.cents + other.cents)

Examples

1. Bare raise
raise NotImplementedError
Returns
NotImplementedError
2. With a message
raise NotImplementedError('export() for PDF')
Returns
NotImplementedError: export() for PDF
3. Subclass forgot to override
class Shape: def area(self): raise NotImplementedError('area') class Square(Shape): pass Square().area()
Returns
NotImplementedError: area
4. abc catches it earlier
from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): ... class Square(Shape): pass Square()
Returns
TypeError: Can't instantiate abstract class Square without an implementation for abstract method 'area'
5. NotImplemented is not an exception
type(NotImplemented).__name__
Returns
'NotImplementedType'
6. Returning NotImplemented from an operator
class Money: def __init__(self, cents): self.cents = cents def __add__(self, other): if not isinstance(other, Money): return NotImplemented return Money(self.cents + other.cents) Money(5) + 1
Returns
TypeError: unsupported operand type(s) for +: 'Money' and 'int'
7. It is a RuntimeError
issubclass(NotImplementedError, RuntimeError)
Returns
True

Pitfalls

1. raise NotImplemented
NotImplemented is a constant, not an exception class. Raising it is itself an error — the real one you meant is NotImplementedError.
raise NotImplemented
class Shape:
    def area(self):
        raise NotImplemented
Shape().area()
TypeError: exceptions must derive from BaseException
raise NotImplementedError
class Shape:
    def area(self):
        raise NotImplementedError('area')
Shape().area()
NotImplementedError: area
2. Raising NotImplementedError from __eq__
Operators expect the NotImplemented return value so Python can fall back (the reflected method, or identity for ==). Raising blows up every comparison with a foreign type — including membership tests in lists.
raise in __eq__
class Money:
    def __init__(self, cents):
        self.cents = cents
    def __eq__(self, other):
        if not isinstance(other, Money):
            raise NotImplementedError
        return self.cents == other.cents
Money(5) == 5
NotImplementedError
return NotImplemented
class Money:
    def __init__(self, cents):
        self.cents = cents
    def __eq__(self, other):
        if not isinstance(other, Money):
            return NotImplemented
        return self.cents == other.cents
Money(5) == 5
False
3. The error surfaces late
A plain base method lets you create the half-finished object; the bug appears only when that code path runs, maybe in production. abc.abstractmethod fails at construction.
Plain base method
class Plugin:
    def run(self):
        raise NotImplementedError
class Broken(Plugin):
    pass
p = Broken()
'created'
'created'
abstractmethod
from abc import ABC, abstractmethod
class Plugin(ABC):
    @abstractmethod
    def run(self): ...
class Broken(Plugin):
    pass
p = Broken()
'created'
TypeError: Can't instantiate abstract class Broken without an implementation for abstract method 'run'

When to use

Use it
  • A base-class method every subclass must override
  • A stub for a feature still being written (with a message saying so)
  • A method that is legitimately unsupported for one specific subclass, e.g. write() on a read-only backend
Reach for something else
  • Binary operators and rich comparisons → return NotImplemented
  • An operation that should never exist on the class → leave it undefined (or set it to None in a subclass)
  • Enforcing overrides at creation time → abc.ABC + @abstractmethod

Notes

NotImplemented
A singleton of type NotImplementedType, returned by __eq__/__add__/… to mean 'try the other operand'
Catch via
except RuntimeError also catches it
docs
The docs say it should not be used for operators or methods not meant to be supported at all

FAQ

NotImplementedError is an exception you raise from a method a subclass must override. NotImplemented is a constant you RETURN from binary operator methods (__eq__, __add__, __lt__, …) to say 'I do not know how to handle this type' — Python then tries the other operand's reflected method and, if that also returns NotImplemented, raises TypeError (or, for ==, falls back to identity). They are not interchangeable: raise NotImplemented is a TypeError, and raising NotImplementedError from __eq__ breaks comparisons.