Comparable classes in Python 3

Neil G picture Neil G · Aug 2, 2011 · Viewed 10.5k times · Source

What is the standard way of making a class comparable in Python 3? (For example, by id.)

Answer

agf picture agf · Aug 2, 2011

To make classes comparable, you only need to implement __lt__ and decorate the class with functools.total_ordering. This provides the rest of the comparison operators so you don't have to write any of them yourself.