__lt__ is the special method Python calls when you use the less-than operator < on your objects. Defining __lt__(self, other) lets a < b work for your class, and it is the only comparison method that sorted(), list.sort(), min(), max() and heapq need. Return True or False when you can compare, and return NotImplemented when you cannot.
This article shows how to implement __lt__ correctly, how to get the other comparison operators with functools.total_ordering or dataclasses, and what mistakes to avoid.
__lt____lt____lt__When Python evaluates a < b, it calls type(a).__lt__(a, b). If that method returns NotImplemented (or does not exist), Python tries the reflected operation on the other side, type(b).__gt__(b, a). If both give up, it raises TypeError.
Comparing tuples is a compact way to implement lexicographic order: Python compares the first elements, and only looks at the second if the first are equal.
Returning NotImplemented instead of raising an error is important. It gives the other object a chance to handle the comparison, and Python then produces a clear TypeError:
__lt__Sorting in Python uses only <. This means one method is enough to make a class sortable.
The same method makes your objects work in a priority queue built with heapq, which also relies on <.
If you only need to sort once by some attribute, a key function is simpler and does not require changing the class:
Defining only __lt__ does not give you <=, >, >=, or reliable ==. You have three good options.
Implement __eq__ and one ordering method, and the decorator fills in the rest:
total_ordering is convenient but adds a small call overhead for the generated methods.
For simple value types, a dataclass can generate all comparisons from the fields in order:
Field order decides the comparison, so list the most significant field first. If you use frozen=True with eq=True, the class is also hashable.
Only do this when the comparison logic differs between operators. In most cases it adds code with no benefit.
__lt__ consistent with __eq__. If two objects are equal, neither should be less than the other.a < b and b < c, then a < c must be true, or sorting gives wrong results without raising an error.NaN and expect a stable sort, because NaN is not less than, greater than or equal to anything.None or unrelated types without handling them. Return NotImplemented rather than letting an AttributeError escape.__eq__ without __hash__, instances become unhashable. Add __hash__ or use a frozen dataclass if you need them in sets or dict keys.A Rectangle ordered by area shows a case where "not less than" does not mean "equal":
Both areas are 6, so neither is less than the other, yet the objects are not the same shape. This is fine for sorting by area, but if you also define __eq__ you must decide whether equal means same area or same dimensions.
__gt__ if I define __lt__?Not for sorting. For a > b, Python can fall back to the reflected b.__lt__(a) when both objects have the same type, so > works for many cases. Use total_ordering or a dataclass if you want all operators to behave predictably.
__lt__ return for an unsupported type?Return NotImplemented, not False. That lets Python try the other operand and raise a clear TypeError.
It uses < only, so __lt__ is enough.
__lt__ with heapq?Yes. heapq compares items with <, so objects that define __lt__ can be pushed onto a heap directly.
__lt__ powers the < operator and is the one method Python needs for sorting, min(), max() and heapq. Compare tuples of fields for lexicographic order, return NotImplemented for unsupported types, and use functools.total_ordering or @dataclass(order=True) when you need the full set of comparison operators.