Skip to content
Advertisement

Tag: comparable

Comparable classes in Python 3

What is the standard way of making a class comparable in Python 3? (For example, by id.) 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

Advertisement