Python __le__ Method
Last modified November 18, 2026 Python 3.15
The <= operator calls __le__ on its left operand.
The method returns a boolean, or NotImplemented when it does not
recognise the other type. This article shows the basic dispatch, mixed-type
comparisons, the reflected __ge__ method, comparing several
fields at once, and reducing boilerplate with total_ordering.
Basic less-than-or-equal comparison
A class defines __le__(self, other) to say when one instance
is less than or equal to another. The example compares two temperatures by
their stored Celsius value.
class Temperature:
def __init__(self, celsius):
self.celsius = celsius
def __le__(self, other):
return self.celsius <= other.celsius
def __repr__(self):
return f"Temperature({self.celsius})"
a = Temperature(20)
b = Temperature(25)
c = Temperature(20)
print(a <= b, b <= a, a <= c)
20 is at most 25 and 20 is at most 20, but 25 is not at most 20.
The hook receives the right-hand object as other and returns a
plain boolean. Notice that <= still works when the two values
are equal — unlike <.
$ python basic_le.py True False True
Mixed types and NotImplemented
A hook can accept more than its own type. When it cannot handle the right
operand it should return NotImplemented, which gives Python a
chance to try the reflected method or, failing that, to raise
TypeError.
class Temperature:
def __init__(self, celsius):
self.celsius = celsius
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 <= 30, t <= 20, t <= 25.0)
try:
print(t <= "x")
except TypeError as e:
print(type(e).__name__, e)
The temperature compares cleanly with integers and floats. For a string,
both Temperature.__le__ and the reflected operation on
str return NotImplemented, so Python raises
TypeError. Returning NotImplemented rather than
False is what makes that graceful failure possible.
$ python mixed_le.py True False True TypeError '<=' not supported between instances of 'Temperature' and 'str'
The reflected __ge__ method
Evaluating 30 <= t would first try
int.__le__(30, t), which does not recognise a
Temperature. Python then tries t.__ge__(30). Define
the reflected method so the comparison works from either side.
class Temperature:
def __init__(self, celsius):
self.celsius = celsius
def __le__(self, other):
if isinstance(other, (int, float)):
return self.celsius <= other
if isinstance(other, Temperature):
return self.celsius <= other.celsius
return NotImplemented
def __ge__(self, other):
if isinstance(other, (int, float)):
return self.celsius >= other
if isinstance(other, Temperature):
return self.celsius >= other.celsius
return NotImplemented
t = Temperature(20)
print(30 >= t, 20 >= t, t >= 30)
print(t <= 30, 30 <= t)
30 >= t is true because 30 is at least 20, and
20 >= t is true because the values are equal. The last line
compares the same pair in both directions: t <= 30 is true,
while 30 <= t is false. Both directions must be implemented
for consistent results.
$ python reflected_le.py True True False True False
Comparing several fields
When ordering depends on more than one attribute, build a tuple and compare that. Tuple comparison stops at the first differing element, which is exactly the rule needed for semantic versions. All comparison methods should be implemented, not left as empty placeholders.
class Version:
def __init__(self, major, minor, patch):
self.major = major
self.minor = minor
self.patch = patch
def _key(self):
return (self.major, self.minor, self.patch)
def __le__(self, other):
if not isinstance(other, Version):
return NotImplemented
return self._key() <= other._key()
def __lt__(self, other):
if not isinstance(other, Version):
return NotImplemented
return self._key() < other._key()
def __eq__(self, other):
if not isinstance(other, Version):
return NotImplemented
return self._key() == other._key()
def __repr__(self):
return f"Version{self._key()}"
v1 = Version(1, 2, 3)
v2 = Version(1, 3, 0)
print(v1 <= v2, v2 <= v1)
print(v1 < v2, v1 == v2)
Version 1.2.3 is less than 1.3.0 because the minor field differs while the
major fields are equal. Keeping the three comparisons consistent —
<=, < and == — is important:
sorting code and user code often mix them.
$ python version_le.py True False True False
Reducing boilerplate with total_ordering
The functools.total_ordering decorator fills in the missing
comparison methods. Supply __eq__ and one of
__lt__, __le__, __gt__ or
__ge__, and the rest are derived.
from functools import total_ordering
@total_ordering
class Person:
def __init__(self, age):
self.age = age
def __le__(self, other):
if not isinstance(other, Person):
return NotImplemented
return self.age <= other.age
def __eq__(self, other):
if not isinstance(other, Person):
return NotImplemented
return self.age == other.age
def __repr__(self):
return f"Person({self.age})"
p1 = Person(30)
p2 = Person(25)
print(p1 <= p2, p2 <= p1)
print(p1 > p2, p1 >= p2, p1 < p2)
Only __le__ and __eq__ were written. The
decorator generated __lt__, __gt__ and
__ge__, which is why the second line works. The generated
methods follow the usual mathematical relationships rather than adding new
rules.
$ python total_ordering_le.py False True True True False
Handling edge cases
A comparison may need a special rule for a sentinel such as
None. Returning False there is a documented choice;
unsupported object types still return NotImplemented.
class Product:
def __init__(self, name, price):
self.name = name
self.price = price
def __le__(self, other):
if other is None:
return False
if not isinstance(other, Product):
return NotImplemented
return self.price <= other.price
def __repr__(self):
return f"Product({self.name}, {self.price})"
book = Product("Book", 15.99)
pen = Product("Pen", 1.99)
print(book <= pen, pen <= book)
print(book <= None)
The cheaper pen is at most the book, and comparing with
None returns False by the explicit rule. Because
None is handled before the type check, the comparison never
reaches NotImplemented for that case.
$ python edge_le.py False True False
The operator.le function
operator.le(a, b) is the functional form of
a <= b. It calls the same hook and is useful when a callable
is needed, for example with filter() or map().
import operator
class Score:
def __init__(self, points):
self.points = points
def __le__(self, other):
return self.points <= other.points
def __repr__(self):
return f"Score({self.points})"
a = Score(10)
b = Score(20)
print(operator.le(a, b))
print(operator.le(2, 3))
print(operator.le(3, 2))
The custom object uses its hook and the built-in integers use theirs. Both
results are ordinary booleans, so operator.le can be passed
around like any other predicate.
$ python operator_le.py True True False
Best practices
A few rules keep <= consistent:
- Return a boolean for supported types and
NotImplementedfor unsupported ones. - Define
__ge__as well sob <= aworks from the reflected side. - Keep
__le__,__lt__and__eq__consistent with one another. - Use tuple keys for multi-field ordering.
- Consider
total_orderingto avoid writing every comparison method by hand.
Source
object.__le__ - Python documentation
Rich comparisons - Python documentation
functools.total_ordering - Python documentation
operator.le - Python documentation
In this article we have implemented the __le__ method. We have
covered the <= dispatch, mixed types, reflected comparison,
multi-field ordering, total_ordering, edge cases and
operator.le. For related topics, see Python
__lt__ method, Python __ge__ method, and
Python __eq__ method.
Author
List all Python tutorials.