Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

object.__le__

The method defines less-than-or-equal and can express a partial order.

Version: Python 3.14; repository image, production runtime not attested

Syntax

left <= right

The method defines less-than-or-equal and can express a partial order.

object.__le__

Python does not infer it from < and == or generate < from it. A reflected __ge__ can handle the comparison, with priority for a strict right-hand subclass. If both sides decline, TypeError follows.

Rights inclusion yields True True False, while the separate < comparison fails.

class Left:
    def __le__(self,other): return NotImplemented
class Right:
    def __ge__(self,other): return 'reflected'
assert (Left()<=Right())=='reflected'
try: Left()<=object()
except TypeError: pass
else: raise AssertionError

Examples

class Rights:
    def __init__(self, names): self.names = set(names)
    def __le__(self, other):
        if not isinstance(other, Rights): return NotImplemented
        return self.names <= other.names
a, b = Rights(["read"]), Rights(["read","write"])
print(a <= a, a <= b, b <= a)
try: a < b
except TypeError as exc: print(type(exc).__name__)

Result

True True False
TypeError

Rights inclusion yields True True False, while the separate < comparison fails.

Sources