NotImplemented
First of all, NotImplemented is not an exception; it’s a built-in constant in Python. It’s a special value that must be returned by the binary special methods – that is, the methods that implement the binary operators of comparison, arithmetic, and bitwise operations (__eq__, __lt__, __add__, etc.) – when the operation is not implemented for the combination of types involved.
What Happens When NotImplemented Is Returned
From the Python documentation:
When a binary (or in-place) method returns
NotImplementedthe interpreter will try the reflected operation on the other type (or some other fallback, depending on the operator). If all attempts returnNotImplemented, the interpreter will raise an appropriate exception. Incorrectly returningNotImplementedwill result in a misleading error message or theNotImplementedvalue being returned to Python code.See Implementing the arithmetic operations for examples.
The “reflected operation on the other type” is just the reverse operation: if a < b returns NotImplemented, the interpreter will try b < a.
NotImplementedError
Now, the NotImplementedError is indeed an exception. Its use is more straightforward: it should be raised when a method is not implemented yet.
From the Python documentation:
This exception is derived from
RuntimeError. 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.