Looking through decimal.py
, it uses NotImplemented
in many special methods. e.g.
class A(object):
def __lt__(self, a):
return NotImplemented
def __add__(self, a):
return NotImplemented
The Python docs say:
NotImplemented
Special value which can be returned by the “rich comparison” special methods (
__eq__()
,__lt__()
, and friends), to indicate that the comparison is not implemented with respect to the other type.
It doesn't talk about other special methods and neither does it describe the behavior.
It seems to be a magic object which if returned from other special methods raises TypeError
, and in “rich comparison” special methods does nothing.
e.g.
print A() < A()
prints True
, but
print A() + 1
raises TypeError
, so I am curious as to what's going on and what is the usage/behavior of NotImplemented.