What is the standard way of making a class comparable in Python 3? (For example, by id.)
Advertisement
Answer
To make classes comparable, you only need to implement __lt__
and decorate the class with functools.total_ordering
. You should also provide an __eq__
method if possible. This provides the rest of the comparison operators so you don’t have to write any of them yourself.