How to Fix NotImplementedError in Python
In this tutorial, you'll learn about How to Fix NotImplementedError in Python. We cover key concepts, practical examples, and best practices.
Python raises NotImplementedError when a method that is meant to be overridden by a subclass has not been implemented.
The Wrong Way
class BaseProcessor:
def process(self, data):
raise NotImplementedError("Subclasses must implement process()")
class CSVProcessor(BaseProcessor):
pass # forgot to implement process()
p = CSVProcessor()
p.process("data")
This produces:
Traceback (most recent call last):
File "script.py", line 9, in <module>
p.process("data")
File "script.py", line 3, in process
raise NotImplementedError("Subclasses must implement process()")
NotImplementedError: Subclasses must implement process()
The Right Way
Implement the required method in the subclass:
class BaseProcessor:
def process(self, data):
raise NotImplementedError("Subclasses must implement process()")
class CSVProcessor(BaseProcessor):
def process(self, data):
return f"Processing CSV: {data}"
p = CSVProcessor()
result = p.process("data")
print(result)
Output:
Processing CSV: data
Step-by-Step Fix
1. Use ABC and abstractmethod for compile-time checking
from abc import ABC, abstractmethod
class BaseProcessor(ABC):
@abstractmethod
def process(self, data):
pass
class CSVProcessor(BaseProcessor):
def process(self, data):
return f"Processing CSV: {data}"
# This will raise TypeError at instantiation if process is not implemented
2. Check for typos in the method name
class CSVProcessor(BaseProcessor):
def proces(self, data): # typo -- missing 's'
pass
3. Implement all required methods
class Shape:
def area(self):
raise NotImplementedError
def perimeter(self):
raise NotImplementedError
class Rectangle(Shape):
def __init__(self, w, h):
self.w = w
self.h = h
def area(self):
return self.w * self.h
def perimeter(self):
return 2 * (self.w + self.h)
r = Rectangle(3, 4)
print(r.area())
print(r.perimeter())
Output:
12
14
Prevention Tips
- Use
abc.ABCand@abstractmethodfor abstract base classes. - Instantiate the subclass early in development to catch missing methods.
- Use a linter to detect missing method implementations.
- Write unit tests that call every method of the interface.
- Use type hints and mypy to verify interface compliance.
Common Mistakes with notimplementederror
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro