When to use 'raise NotImplementedError'?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Python, the `NotImplementedError` is a unique exception class used by developers to signal that a specific piece of code is intentionally unimplemented. This exception is often utilized in object-oriented programming and is particularly valuable in the context of inheritance and subclassing. In this article, we’ll delve into the technical details of when and why you should use `raise NotImplementedError` in your code, and explore the best practices surrounding this Python feature.
Understanding `NotImplementedError`
What is `NotImplementedError`?
`NotImplementedError` is an exception derived from the built-in `Exception` class in Python. Unlike many other exceptions that are raised when something goes wrong, the `NotImplementedError` indicates that a portion of the program or method lacks an implementation on purpose. This exception is especially significant in designing framework-like structures where certain classes are intended to act as blueprints for future extensions.
Key Use Cases
- Abstract Methods in Base Classes: When creating a class intended to be a base class, you may want certain methods to be overridden by child classes. In such scenarios, you can define a method in the base class that raises `NotImplementedError`, signaling that any subclass must provide its own implementation for this method.
- Partially Implemented Interfaces: In some cases, an interface or an abstract class may have methods that are not immediately necessary, yet might be in the future. By raising `NotImplementedError`, you anticipate potential extensions while maintaining a placeholder for future functionality.
- Development Placeholders: During the development phase, you may encounter situations where you want to test parts of your code while others are not yet ready. In this case, raising `NotImplementedError` can serve as a temporary marker.
Technical Explanation
Consider the following pattern that employs `NotImplementedError` within an abstract base class:
- Abstract Base Classes: Use `NotImplementedError` to force subclasses to implement specific methods.
- Interfaces: When creating custom interface-like structures where some methods are declared but not defined.
- Prototypes and MVPs: When working on minimal viable products and evolving prototypes to signal ongoing development areas.
- Unintended Omissions: Avoid using `NotImplementedError` when you simply haven’t implemented a function due to oversight or prioritization.
- Finished Code: Do not leave `NotImplementedError` in your final production code unless it serves a purpose in your code architecture. It could lead to runtime exceptions and disrupt the user experience.
Related reading
- When using run_in_executor in asyncio, is the event loop executed in the main thread?
- When would the -e, --editable option be useful with pip install?
- Where can I find source or algorithm of Python's hash function?
- Where do the Python unit tests go?
- When to use the different log levels
- Where are AWS CodeDeploy Deployment logs found?
- Where does pip install its packages?
- Where does pip install its packages?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.