__gt__Easy Examples

Defines behavior for the > greater-than operator

Implementing __gt__

Basic implementation of __gt__ in a class.

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

obj = Example()
print(obj)

__gt__ defines behavior for the > greater-than operator. Implementing it lets you customize how Python interacts with your objects.

__gt__ in action

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

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

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

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

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

Want to try these examples interactively?

Open Easy Playground