Looking through decimal.py
, it uses NotImplemented
in many special methods. e.g.
class A(object):
def __lt__(self, a):
It actually has the same meaning when returned from __add__
as from __lt__
, the difference is Python 2.x is trying other ways of comparing the objects before giving up. Python 3.x does raise a TypeError. In fact, Python can try other things for __add__
as well, look at __radd__
and (though I'm fuzzy on it) __coerce__
.
# 2.6
>>> class A(object):
... def __lt__(self, other):
... return NotImplemented
>>> A() < A()
True
# 3.1
>>> class A(object):
... def __lt__(self, other):
... return NotImplemented
>>> A() < A()
Traceback (most recent call last):
File "", line 1, in
TypeError: unorderable types: A() < A()
See Ordering Comparisions (3.0 docs) for more info.