Python NotImplementedError Exception occurs at runtime when client characterized base classes; conceptual techniques should raise this exception when they require inferred classes to abrogate the strategy or while the class is being created to demonstrate that the genuine usage despite everything should be included.
User-defined base classes can raise NotImplementedError to indicate that a method or behavior needs to be defined by a subclass, simulating an interface. This exception is derived from RuntimeError.
According to Python, the NotImplementedError occurs when an abstract method lacks the required derived class to override this method, thus raising this exception.
The NotImplementedError is raised when you do not implement the abstract method in the child class. This error can be solved by using the abstract method in every child class. If we use the abstract method inside the child class, a new instance of that method is created inside the child class.
As the documentation states [docs],
In user defined base classes, abstract methods should raise this exception when they require derived classes to override the method, or while the class is being developed to indicate that the real implementation still needs to be added.
Note that although the main stated use case this error is the indication of abstract methods that should be implemented on inherited classes, you can use it anyhow you'd like, like for indication of a TODO
marker.
As Uriel says, it is meant for a method in an abstract class that should be implemented in child class, but can be used to indicate a TODO as well.
There is an alternative for the first use case: Abstract Base Classes. Those help creating abstract classes.
Here's a Python 3 example:
class C(abc.ABC):
@abc.abstractmethod
def my_abstract_method(self, ...):
...
When instantiating C
, you'll get an error because my_abstract_method
is abstract. You need to implement it in a child class.
TypeError: Can't instantiate abstract class C with abstract methods my_abstract_method
Subclass C
and implement my_abstract_method
.
class D(C):
def my_abstract_method(self, ...):
...
Now you can instantiate D
.
C.my_abstract_method
does not have to be empty. It can be called from D
using super()
.
An advantage of this over NotImplementedError
is that you get an explicit Exception
at instantiation time, not at method call time.
Consider if instead it was:
class RectangularRoom(object):
def __init__(self, width, height):
pass
def cleanTileAtPosition(self, pos):
pass
def isTileCleaned(self, m, n):
pass
and you subclass and forget to tell it how to isTileCleaned()
or, perhaps more likely, typo it as isTileCLeaned()
. Then in your code, you'll get a None
when you call it.
None
valid output? Who knows. raise NotImplmentedError
forces you to implement it, as it will throw an exception when you try to run it until you do so. This removes a lot of silent errors. It's similar to why a bare except is almost never a good idea: because people make mistakes and this makes sure they aren't swept under the rug.
Note: Using an abstract base class, as other answers have mentioned, is better still, as then the errors are frontloaded and the program won't run until you implement them (with NotImplementedError, it will only throw an exception if actually called).
One could also do a raise NotImplementedError()
inside the child method of an @abstractmethod
-decorated base class method.
Imagine writing a control script for a family of measurement modules (physical devices). The functionality of each module is narrowly-defined, implementing just one dedicated function: one could be an array of relays, another a multi-channel DAC or ADC, another an ammeter etc.
Much of the low-level commands in use would be shared between the modules for example to read their ID numbers or to send a command to them. Let's see what we have at this point:
from abc import ABC, abstractmethod #< we'll make use of these later
class Generic(ABC):
''' Base class for all measurement modules. '''
# Shared functions
def __init__(self):
# do what you must...
def _read_ID(self):
# same for all the modules
def _send_command(self, value):
# same for all the modules
We then realise that much of the module-specific command verbs and, therefore, the logic of their interfaces is also shared. Here are 3 different verbs whose meaning would be self-explanatory considering a number of target modules.
get(channel)
relay: get the on/off status of the relay on channel
DAC: get the output voltage on channel
ADC: get the input voltage on channel
enable(channel)
relay: enable the use of the relay on channel
DAC: enable the use of the output channel on channel
ADC: enable the use of the input channel on channel
set(channel)
relay: set the relay on channel
on/off
DAC: set the output voltage on channel
ADC: hmm... nothing logical comes to mind.
I'd argue that there is a strong case for the above verbs to be shared across the modules
as we saw that their meaning is evident for each one of them. I'd continue writing my
base class Generic
like so:
class Generic(ABC): # ...continued
@abstractmethod
def get(self, channel):
pass
@abstractmethod
def enable(self, channel):
pass
@abstractmethod
def set(self, channel):
pass
We now know that our subclasses will all have to define these methods. Let's see what it could look like for the ADC module:
class ADC(Generic):
def __init__(self):
super().__init__() #< applies to all modules
# more init code specific to the ADC module
def get(self, channel):
# returns the input voltage measured on the given 'channel'
def enable(self, channel):
# enables accessing the given 'channel'
You may now be wondering:
But this won't work for the ADC module as
set
makes no sense there as we've just seen this above!
You're right: not implementing set
is not an option as Python would then fire the error below
when you tried to instantiate your ADC object.
TypeError: Can't instantiate abstract class 'ADC' with abstract methods 'set'
So you must implement something, because we made set
an enforced verb (aka '@abstractmethod'),
which is shared by two other modules but, at the same time, you must also not implement anything as
set
does not make sense for this particular module.
By completing the ADC class like this:
class ADC(Generic): # ...continued
def set(self, channel):
raise NotImplementedError("Can't use 'set' on an ADC!")
You are doing three very good things at once:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With