Python __ge__ Method
Last modified November 17, 2026 Python 3.15
The >= operator calls the __ge__ special
method on its left operand. A well-behaved implementation returns
NotImplemented for types it does not recognise, so Python can try
the reflected __le__ method on the right operand. This article
shows that dispatch, mixed-type comparisons, the
functools.total_ordering shortcut, reflected fallback, and
inheritance.
How >= calls __ge__
Python evaluates a >= b as
type(a).__ge__(a, b). The method is looked up on the type, so an
instance attribute with that name is ignored. It should return a boolean, or
NotImplemented when it does not recognise the other operand. The
example compares distances in both directions, tests the reflexive case, and
calls the hook directly.
class Distance:
def __init__(self, meters):
self.meters = meters
def __ge__(self, other):
if not isinstance(other, Distance):
return NotImplemented
return self.meters >= other.meters
def __repr__(self):
return f"Distance({self.meters})"
d1 = Distance(100)
d2 = Distance(50)
print(d1 >= d2)
print(d2 >= d1)
print(d1 >= d1)
print(Distance.__ge__(d1, d2))
The first two results show that direction matters: 100 is at least 50,
but 50 is not at least 100. The third line confirms the operation is reflexive
for these objects. The final call to Distance.__ge__(d1, d2)
matches d1 >= d2, confirming the dispatch.
$ python basic_ge.py True False True True
Mixed-type comparisons
A comparison method may accept more than one type. Here
Temperature compares with other temperatures and with plain
numbers. Returning NotImplemented for anything else lets Python
try the reflected operation or, failing that, raise TypeError.
class Temperature:
def __init__(self, celsius):
self.celsius = celsius
def __ge__(self, other):
if isinstance(other, Temperature):
return self.celsius >= other.celsius
if isinstance(other, (int, float)):
return self.celsius >= other
return NotImplemented
def __le__(self, other):
if isinstance(other, Temperature):
return self.celsius <= other.celsius
if isinstance(other, (int, float)):
return self.celsius <= other
return NotImplemented
t = Temperature(25)
print(t >= Temperature(20))
print(t >= 30)
print(15 >= t)
try:
print(t >= "warm")
except TypeError as e:
print(type(e).__name__, e)
The first two lines compare against a temperature and a number. In
15 >= t the left operand is an int, whose own
comparison declines the unknown type, so Python calls
t.__le__(15); the result is False because 25 is not
at most 15. Comparing with a string reaches no working method and raises
TypeError, which the example catches.
$ python mixed_types.py True False False TypeError '>=' not supported between instances of 'Temperature' and 'str'
Reducing boilerplate with total_ordering
The functools.total_ordering decorator fills in the missing
comparison methods from one complete ordering method and __eq__.
Supplying __ge__ and __eq__ is enough to get
<, <=, and > automatically.
from functools import total_ordering
@total_ordering
class Version:
def __init__(self, major, minor):
self.major = major
self.minor = minor
def __eq__(self, other):
if not isinstance(other, Version):
return NotImplemented
return (self.major, self.minor) == (other.major, other.minor)
def __ge__(self, other):
if not isinstance(other, Version):
return NotImplemented
return (self.major, self.minor) >= (other.major, other.minor)
def __repr__(self):
return f"Version({self.major}, {self.minor})"
v1 = Version(1, 2)
v2 = Version(1, 3)
print(v1 >= v2)
print(v1 <= v2)
print(v1 > v2)
print(v1 < v2)
The class defines only __eq__ and __ge__, yet all
four comparisons work. Note that __eq__ returns
NotImplemented for unrelated types instead of False;
that keeps equality symmetric when the other operand has its own rule. Tuple
comparison handles the major and minor fields in order.
$ python total_ordering.py False True False True
The reflected __le__ method
For a >= b, the reflected method is b.__le__(a),
not b.__ge__(a). Python tries it when the left operand's
__ge__ returns NotImplemented. Adding
__le__ therefore makes number >= custom work.
class Weight:
def __init__(self, kg):
self.kg = kg
def __ge__(self, other):
print("__ge__ called")
if isinstance(other, Weight):
return self.kg >= other.kg
if isinstance(other, (int, float)):
return self.kg >= other
return NotImplemented
def __le__(self, other):
print("__le__ called")
if isinstance(other, Weight):
return self.kg <= other.kg
if isinstance(other, (int, float)):
return self.kg <= other
return NotImplemented
w = Weight(50)
print(w >= 40)
print(60 >= w)
try:
print("50" >= w)
except TypeError as e:
print(type(e).__name__, e)
The printed names show which hook ran. w >= 40 uses
__ge__ on the left, while 60 >= w uses
__le__ on the right. With a string on the left, str
declines and Weight.__le__ also returns
NotImplemented, so Python raises TypeError.
$ python reflected.py __ge__ called True __le__ called True __le__ called TypeError '>=' not supported between instances of 'str' and 'Weight'
Inheritance
A base class that checks with isinstance accepts instances of
its subclasses. A subclass can override the method to add a tie-breaker and
delegate other cases to super().
class Animal:
def __init__(self, name, weight):
self.name = name
self.weight = weight
def __ge__(self, other):
if not isinstance(other, Animal):
return NotImplemented
return self.weight >= other.weight
def __repr__(self):
return f"{type(self).__name__}({self.name!r}, {self.weight})"
class Dog(Animal):
def __ge__(self, other):
if not isinstance(other, Dog):
return super().__ge__(other)
if self.weight == other.weight:
return self.name >= other.name
return self.weight >= other.weight
a = Animal("generic", 10)
d1 = Dog("Rex", 15)
d2 = Dog("Ace", 15)
print(d1 >= d2)
print(d2 >= d1)
print(d1 >= a)
Two dogs with the same weight are compared by name, so the alphabetically
later name wins. When the other operand is a plain Animal,
Dog.__ge__ delegates to super().__ge__(other) and
the weight rule applies. Comparing only fields that both classes share keeps
the relation consistent across the hierarchy.
$ python inheritance.py True False True
Best practices
Keep comparisons predictable with these habits:
- Return
NotImplementedfor types you do not handle. - Implement the reflected method so your type works on the right of the operator.
- Keep
>=consistent with<=,>, and<. - Use
total_orderingwhen you have one ordering method and__eq__. - Document which field or fields decide the order.
Source
object.__ge__ - Python documentation
object.__le__ - Python documentation
functools.total_ordering - Python documentation
In this article we have implemented the __ge__ method. We have
covered the >= dispatch, mixed types,
total_ordering, the reflected __le__ hook, and
inheritance. For related topics, see
Python __gt__ method,
Python __le__ method, and
Python __eq__ method.
Author
List all Python tutorials.