Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Are __lt__ and __gt__ Methods in Python?

Python
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedJul 13, 2023
UpdatedNov 17, 2026
5 min read

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.

Table of Contents

  • How the less-than and greater-than methods fit into rich comparison
  • Implementing less-than and greater-than in a class
  • Sorting and using min and max with your class
  • Getting all six comparisons with functools.total_ordering
  • Alternatives for modern code
  • Common mistakes
  • Frequently Asked Questions
    • Do I need to define both methods?
    • What happens if a comparison returns NotImplemented?
    • Is the old cmp method still available?
    • Should I use total_ordering or a dataclass?
  • Summary

How the less-than and greater-than methods fit into rich comparison

Python has six rich comparison methods. Each one maps to one operator:

OperatorMethodReflected 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.

Implementing less-than and greater-than in a class

The example below defines a Version class that compares by its numeric parts.

python
text

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.

Sorting and using min and max with your class

list.sort(), sorted(), min(), max() and the heapq module only use the < operator, so defining __lt__ is enough to make your objects sortable.

python
text

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.

Getting all six comparisons with functools.total_ordering

Writing 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__.

python
text

The generated methods are slightly slower than hand-written ones. For performance-critical classes, or when you want exact control, write the methods yourself.

Alternatives for modern code

For simple value classes, a dataclass with order=True generates all comparison methods from the fields, in field order:

python
text

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.

Common mistakes

  1. Raising an exception for unknown types instead of returning NotImplemented.
  2. Defining __eq__ without keeping it consistent with __lt__, which gives surprising results in sorted collections.
  3. Defining __eq__ without __hash__. A class that defines __eq__ and not __hash__ becomes unhashable, so it cannot be a dictionary key or set member.
  4. Comparing floats or mutable fields that change after the object is stored in a sorted structure.

Frequently Asked Questions

Do I need to define both methods?

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.

What happens if a comparison returns NotImplemented?

Python tries the reflected method on the right operand. If that also returns NotImplemented, a TypeError is raised.

Is the old cmp method still available?

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.

Should I use 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__.

Summary

__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.

Share