The __ge__ method is the special (dunder) method Python calls for the greater-than-or-equal operator >=. When you write a >= b, Python runs a.__ge__(b). Implement it to make instances of your class comparable, and return True, False, or NotImplemented when you cannot compare with the other type.
Python 3 has six rich comparison methods: __lt__, __le__, __eq__, __ne__, __gt__ and __ge__. You rarely need to write all of them by hand, because functools.total_ordering and dataclasses can generate the rest.
__ge____ge__The signature takes the right-hand operand and returns a value, normally a bool:
Python evaluates a >= b in this order:
type(b) is a subclass of type(a) and overrides the reflected method, Python tries b.__le__(a) first.a.__ge__(b).NotImplemented, it tries the reflected method b.__le__(a).NotImplemented, Python raises TypeError: '>=' not supported between instances of ....The reflected pair of __ge__ is __le__, not __ge__. That is easy to forget when you debug mixed-type comparisons.
| Operator | Method | Reflected method |
|---|---|---|
a < b | __lt__ | __gt__ |
a <= b | __le__ | __ge__ |
a > b | __gt__ | __lt__ |
a >= b | __ge__ | __le__ |
Comparing tuples gives you correct lexicographic ordering with very little code. This Version class compares by major and then minor number.
The isinstance check keeps the method safe. Comparing a Version with an unrelated object returns NotImplemented, and Python raises a clear TypeError instead of an AttributeError from inside your method.
If you skip the type check, Version(1, 2) >= 5 fails with an AttributeError because 5 has no major attribute. With NotImplemented, Python gives the other operand a chance to handle the comparison, which is how mixed comparisons such as int with float work.
Never return NotImplemented from a method you call directly in your own code, such as v.__ge__(5). It is a signal to the interpreter, and it is truthy in older versions of Python. Using it as a boolean is deprecated since Python 3.9 and raises a TypeError in Python 3.14.
Writing every method is repetitive. functools.total_ordering fills in the missing ones when you define __eq__ and at least one of __lt__, __le__, __gt__ or __ge__.
The decorator derives >, < and <= from __eq__ and __ge__. It is convenient, but the generated methods are slower than hand-written ones, so measure before using it on hot paths.
For plain data holders, @dataclass(order=True) generates all four ordering methods from the fields in declaration order. No manual __ge__ is needed.
Use a hand-written __ge__ when ordering depends on a computed value, such as comparing rectangles by width * height, or when you want custom rules for mixed types.
__ge__ but not __eq__. Objects then compare equal only by identity, which gives surprising results for >= on equal values.__eq__ without __hash__. Python sets __hash__ to None, so the object is no longer usable in sets or as a dict key.nan. Every ordering comparison with nan is False, so >= and < can both be False.__ge__ inconsistent with __gt__ and __eq__. Sorting and bisect rely on a consistent total order.sorted() on objects that only define __ge__. Sorting uses __lt__, so define that too (or use total_ordering).__ge__?No. sorted() and list.sort() use only __lt__. Define __lt__ (or use total_ordering) for sortable objects.
__ge__?a >= b works, but a > b raises TypeError unless the right operand defines the reflected __lt__. Use total_ordering to fill the gaps.
__ge__ return a bool?Normally yes. Libraries such as NumPy and pandas return arrays from comparisons on purpose, but ordinary classes should return True or False.
Yes, if you handle them explicitly. Check the type of other and either convert it or return NotImplemented.
__ge__ powers the >= operator. Return a bool for supported types and NotImplemented for everything else, keep it consistent with __eq__ and the other ordering methods, and use functools.total_ordering or @dataclass(order=True) to avoid writing all six methods yourself.