NotImplementedError
NotImplementedError is a built-in exception that signals a method or function hasn’t been implemented yet.
NotImplementedError is typically raised in a user-defined base class or interface to signal that subclasses must provide their own implementations. In a true abstract base class (ABC) that uses @abstractmethod, Python already prevents instantiation with a TypeError, so NotImplementedError is the runtime signal for base classes that don’t rely on abc. This exception also helps flag code that’s intentionally incomplete during development.
NotImplementedError isn’t interchangeable with the built-in NotImplemented constant: NotImplemented is the value that binary special methods like .__eq__() and .__add__() return to signal that an operation isn’t supported for the other operand’s type, while NotImplementedError is an exception that gets raised.
NotImplementedError Occurs When
- Calling a method that explicitly raises this exception to indicate that its implementation doesn’t exist yet
NotImplementedError Can Be Used When
- Indicating that a method in a base class needs to be implemented in a subclass
- Serving as a placeholder for future code development
- Alerting developers to incomplete sections of code during development
NotImplementedError Example
An example of when the exception appears:
>>> class Animal:
... def speak(self):
... raise NotImplementedError(
... "Subclasses must implement this method"
... )
...
>>> class Dog(Animal):
... pass
...
>>> dog = Dog()
>>> dog.speak()
Traceback (most recent call last):
...
NotImplementedError: Subclasses must implement this method
NotImplementedError How to Fix It
To fix code that raises NotImplementedError, implement the missing method in the subclass:
>>> class Animal:
... def speak(self):
... raise NotImplementedError(
... "Subclasses must implement this method"
... )
...
>>> class Dog(Animal):
... def speak(self):
... print("Woof!")
...
>>> dog = Dog()
>>> dog.speak()
Woof!
Related Resources
Course
Using raise for Effective Exceptions
In this video course, you'll learn how to raise exceptions in Python, which will improve your ability to efficiently handle errors and exceptional situations in your code. This way, you'll write more reliable, robust, and maintainable code.
For additional information on related topics, take a look at the following resources:
- Python's raise: Effectively Raising Exceptions in Your Code (Tutorial)
- Implementing Interfaces in Python: ABCs and Protocols (Tutorial)
- Python's Built-in Exceptions: A Walkthrough With Examples (Tutorial)
- Python Exceptions: An Introduction (Tutorial)
- Object-Oriented Programming (OOP) in Python (Tutorial)
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Python's raise: Effectively Raising Exceptions in Your Code (Quiz)
- Python Interfaces: Object-Oriented Design Principles (Course)
- Implementing Interfaces in Python: ABCs and Protocols (Quiz)
- Working With Python's Built-in Exceptions (Course)
- Python's Built-in Exceptions: A Walkthrough With Examples (Quiz)
- Introduction to Python Exceptions (Course)
- Raising and Handling Python Exceptions (Course)
- Python Exceptions: An Introduction (Quiz)
- Intro to Object-Oriented Programming (OOP) in Python (Course)
- A Conceptual Primer on OOP in Python (Course)
- Object-Oriented Programming (OOP) in Python (Quiz)
- Class Concepts: Object-Oriented Programming in Python (Course)
- Inheritance and Internals: Object-Oriented Programming in Python (Course)
- Python Classes - The Power of Object-Oriented Programming (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.