The __lt__ and __gt__ methods are Python's special (dunder) methods for the less-than (<) and greater-than (>) operators. When you write a < b, Python calls a.__lt__(b); when you write a > b, it calls a.__gt__(b). Implementing them lets instances of your own classes be compared, sorted and used with min() and max().
Both methods should return a boolean, or NotImplemented when they do not know how to compare with the other operand. Returning NotImplemented lets Python try the reflected method on the other object instead of failing straight away.
functools.total_orderingPython has six rich comparison methods. Each one maps to one operator:
| Operator | Method | Reflected method (tried on the right operand) |
|---|---|---|
< | __lt__ | __gt__ |
<= | __le__ | __ge__ |
> | __gt__ | __lt__ |
>= | __ge__ | __le__ |
== | __eq__ | __eq__ |
!= | __ne__ | __ne__ |
For a < b, Python first calls a.__lt__(b). If that returns NotImplemented, it tries b.__gt__(a). If both return NotImplemented, Python raises TypeError. This is why a well-written comparison method returns NotImplemented for unsupported types instead of raising an error itself.
The example below defines a Version class that compares by its numeric parts.
The last line fails because Version.__lt__ returns NotImplemented for an int, and int.__gt__ does not know about Version either. Python then raises a clear TypeError, which is the correct behaviour for an unsupported comparison.
list.sort(), sorted(), min(), max() and the heapq module only use the < operator, so defining __lt__ is enough to make your objects sortable.
max() works here because it also relies on < internally. Implementing __gt__ is still worthwhile when callers write a > b directly, although Python can fall back to the reflected __lt__ of the other operand when both sides are the same type.
functools.total_orderingWriting every comparison method by hand is repetitive. The functools.total_ordering decorator fills in the missing ones when you define __eq__ and one of __lt__, __le__, __gt__ or __ge__.
The generated methods are slightly slower than hand-written ones. For performance-critical classes, or when you want exact control, write the methods yourself.
For simple value classes, a dataclass with order=True generates all comparison methods from the fields, in field order:
If you only need to sort by one attribute and do not want to change the class, pass a key function instead: sorted(items, key=lambda item: item.value). This keeps the comparison logic out of the class entirely.
NotImplemented.__eq__ without keeping it consistent with __lt__, which gives surprising results in sorted collections.__eq__ without __hash__. A class that defines __eq__ and not __hash__ becomes unhashable, so it cannot be a dictionary key or set member.Not always. Sorting only needs __lt__. If you define only __lt__, then a > b still works when Python can use the reflected b.__lt__(a) on the other operand, but defining both makes the behaviour explicit.
NotImplemented?Python tries the reflected method on the right operand. If that also returns NotImplemented, a TypeError is raised.
No. __cmp__ and the cmp() built-in were removed in Python 3. Use the rich comparison methods or functools.cmp_to_key to adapt an old comparison function.
total_ordering or a dataclass?Use @dataclass(order=True) when the comparison is just the fields in order. Use total_ordering when you need custom logic in __eq__ and __lt__.
__lt__ and __gt__ customise the < and > operators. Return a boolean for supported types and NotImplemented for everything else. Defining __lt__ is enough for sorting and min()/max(), functools.total_ordering fills in the remaining comparisons, and @dataclass(order=True) covers simple field-by-field ordering.