watch this The wheels are turning, slowly turning. home
How to override comparison operators in Python 2007-07-13

Python, like many languages, allows the behavior of operators to be
customized using a scheme based on the types of objects they are applied to.
The precise rules and intricacies of this customization are fairly involved,
though, and most people are unaware of their full scope. While it is sometimes
valuable to be able to control the behavior of an operator to the full extent
supported by Python, quite often the complexity which this results in spills
over into simpler applications. This is visible as a general tendency on the
part of Python programmers to implement customizations which are correct for
the narrow case which they have in mind at the moment, but are incorrect when
considered in a broader context. Since many parts of the runtime and standard
library rely on the behavior of these operators, this is a somewhat more
egregious than the case of a similar offense made in an application-specific
method, where the author can simply claim that behavior beyond what was
intended is unsupported and behaves in an undefined manner.



So, with my long-winded introduction out of the way, here are the basic
rules for the customization of ==, !=, <, >, <=, and >=:





So how should these be applied? This is best explained with an example.
While __cmp__ is often useful, I am going to ignore it for the
rest of this post, since it is easier to get right, particularly once
NotImplemented (which I will talk about) is understood.



class A(object):
    def __init__(self, foo):
        self.foo = foo
    def __eq__(self, other):
        if isinstance(other, A):
            return self.foo == other.foo
        return NotImplemented
    def __ne__(self, other):
        result = self.__eq__(other)
        if result is NotImplemented:
            return result
        return not result



That’s it (because I’m not going to define the other four methods to make
<, >, <=, and >= work. They follow basically the same rules as
__eq__ and __ne__, though). Pretty straightforward,
but there are some points which are not always obvious:





The major remaining point is NotImplemented: what is that
thing? NotImplemented signals to the runtime that it should ask
someone else to satisfy the operation. In the expression a == b,
if a.__eq__(b) returns NotImplemented, then Python
tries b.__eq__(a). If b knows enough to return True or False,
then the expression can succeed. If it doesn’t, then the runtime will fall
back to the built-in behavior (which is based on identity for == and !=).



Here’s another class which customizes equality:



class B(object):
    def __init__(self, bar):
        self.bar = bar
    def __eq__(self, other):
        if isinstance(other, B):
            return self.bar == other.bar
        elif isinstance(other, A):
            return self.bar + 3 == other.foo
        else:
            return NotImplemented
    def __ne__(self, other):
        result = self.__eq__(other)
        if result is NotImplemented:
            return result
        return not result



Here we have a class which can compare instances of itself to both instances
itself and to instances of A. Now, what would happen if we weren’t careful
about returning NotImplemented at the right times?



One way it might go is…



>>> class A(object):
...     def __init__(self, foo):
...             self.foo = foo
...     def __eq__(self, other):
...             return self.foo == other.foo
...
>>> class B(object):
...     def __init__(self, bar):
...             self.bar = bar
...
>>> A(5) == B(6)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 5, in __eq__
AttributeError: 'B' object has no attribute 'foo'
>>>



Another way it could go is…



>>> class A(object):
...     def __init__(self, foo):
...             self.foo = foo
...     def __eq__(self, other):
...             if isinstance(other, A):
...                     return self.foo == other.foo
...
>>> class B(object):
...     def __init__(self, bar):
...             self.bar = bar
...     def __eq__(self, other):
...             if isinstance(other, A):
...                     return self.bar + 3 == other.foo
...             else:
...                     return self.bar == other.bar
...
>>> print A(3) == B(0)
None
>>> print B(0) == A(3)
True
>>>



That one’s particularly nasty. ;) But here’s what we get with correct
NotImplemented use:



>>> class A(object):
...     def __init__(self, foo):
...             self.foo = foo
...     def __eq__(self, other):
...             if isinstance(other, A):
...                     return self.foo == other.foo
...             return NotImplemented
...
>>> class B(object):
...     def __init__(self, bar):
...             self.bar = bar
...     def __eq__(self, other):
...             if isinstance(other, A):
...                     return self.bar + 3 == other.foo
...             elif isinstance(other, B):
...                     return self.bar == other.bar
...             else:
...                     return NotImplemented
...
>>> print A(3) == B(0)
True
>>> print B(0) == A(3)
True
>>>



Ahh, excellent. NotImplemented has uses for other operators in
Python as well. For example, if the + override, __add__, returns
it, then __radd__ is tried on the right-hand argument. These can
be useful as well, though equality and inequality are by far more common use
cases.



If you follow these examples, then in the general case you’ll find yourself
with more consistently behaving objects. You may even want to implement a
mixin which provides the __ne__ implementation (and one of
__lt__ or __gt__), since it gets pretty boring typing
that out after a few times. ;)



Of course, there are plenty of special cases where it makes sense to deviate
from this pattern. However, they are special. For most objects, this
is the behavior you want.



You can read about all the gory details of Python’s operator overloading
system on the Python website:
http://docs.python.org/ref/specialnames.html