__le__Easy Examples

Defines behavior for the <= operator

Implementing __le__

Basic implementation of __le__ in a class.

python
class Example:
    def __le__(self):
        return "Example __le__"

obj = Example()
print(obj)

__le__ defines behavior for the <= operator. Implementing it lets you customize how Python interacts with your objects.

__le__ in action

Seeing __le__ called by Python's built-in operations.

python
# How Python calls __le__ automatically
class Demo:
    def __init__(self, value):
        self.value = value

    def __le__(self, other):
        print(f"__le__ was called!")
        return self

d = Demo(42)
# This triggers __le__:
print(d)

Python automatically calls __le__ when you use the corresponding operator or function on your object.

Want to try these examples interactively?

Open Easy Playground