Making a python user-defined class sortable, hashable

There are a few ways of marking your object sortable. First - rich comparison, defined by a set of functions:

object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)

Also it is possible to define only one function:

object.__cmp__(self, other)

And the last should be defined if you want to define custom __hash__ function. See the doc.


I almost posted this as a comment to the other answers but it's really an answer in and of itself.

To make your items sortable, they only need to implement __lt__. That's the only method used by the built in sort.

The other comparisons or functools.total_ordering are only needed if you actually want to use the comparison operators with your class.

To make your items hashable, you implement __hash__ as others noted. You should also implement __eq__ in a compatible way -- items that are equivalent should hash the same.


There isn't any difference between Python 2 and 3.

For sortability:

You should define comparision methods. This makes your items sortable. Generally, you shouldn't prefer __cmp__().

I usually use functools.total_ordering decorator.

functools.total_ordering(cls) Given a class defining one or more rich comparison ordering methods, this class decorator supplies the rest. This simplifies the effort involved in specifying all of the possible rich comparison operations:

The class must define one of __lt__(), __le__(), __gt__(), or __ge__(). In addition, the class should supply an __eq__() method.

You should be careful that your comparison methods do not have any side effects. (change any of the values of the object)

For hashing:

You should implement __hash__() method. I think the best way is returning hash(repr(self)), so your hash would be unique.