ZetCode

Python __gt__ Method

Last modified November 17, 2026 Python 3.15

The > operator calls __gt__ on its left operand. When that hook declines, Python tries __lt__ on the right operand with the arguments swapped. This article shows the dispatch, mixed types, reflected comparison, inheritance, and the functools.total_ordering shortcut.

How > calls __gt__

a > b is evaluated as type(a).__gt__(a, b). The hook returns True or False, or NotImplemented when the other type is not supported. The example compares products by price in both directions and calls the method directly.

basic_gt.py
class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = price

    def __gt__(self, other):
        if not isinstance(other, Product):
            return NotImplemented
        return self.price > other.price


p1 = Product("Laptop", 999)
p2 = Product("Phone", 699)
print(p1 > p2)
print(p2 > p1)
print(Product.__gt__(p1, p2))

The laptop is greater because its price is higher; reversing the operands flips the answer. The direct call matches p1 > p2, confirming the dispatch. Returning NotImplemented for unrelated types would let Python try the reflected comparison instead of raising immediately.

$ python basic_gt.py
True
False
True

Mixed types

A comparison can accept both instances of its own class and built-in numbers, while declining strings. The example keeps the numeric case explicit and catches the TypeError for the unsupported one.

mixed_types.py
class Distance:
    def __init__(self, meters):
        self.meters = meters

    def __gt__(self, other):
        if isinstance(other, Distance):
            return self.meters > other.meters
        if isinstance(other, (int, float)):
            return self.meters > other
        return NotImplemented


d = Distance(100)
print(d > Distance(50))
print(d > 75)
try:
    print(d > "100")
except TypeError as e:
    print(type(e).__name__, e)

Comparing with another Distance and with 75 both succeed. Comparing with a string makes __gt__ return NotImplemented; no reflected method answers either, so Python raises TypeError. Catching it keeps the example runnable.

$ python mixed_types.py
True
True
TypeError '>' not supported between instances of 'Distance' and 'str'

The reflected __lt__

For a > b, the reflected method is b.__lt__(a). Defining __lt__ therefore determines what happens when a built-in value appears on the left. The printed method names make the choice visible.

reflected.py
class Temperature:
    def __init__(self, celsius):
        self.celsius = celsius

    def __gt__(self, other):
        print("__gt__ called")
        if isinstance(other, Temperature):
            return self.celsius > other.celsius
        return NotImplemented

    def __lt__(self, other):
        print("__lt__ called")
        if isinstance(other, (int, float)):
            return self.celsius < other
        return NotImplemented


t = Temperature(25)
print(t > Temperature(20))
print(30 > t)

t > Temperature(20) uses __gt__ on the left object. In 30 > t, the integer's own comparison cannot handle a Temperature, so Python calls t.__lt__(30) and the comparison succeeds. Implementing both methods keeps the relation consistent in either order.

$ python reflected.py
__gt__ called
True
__lt__ called
True

Inheritance

A subclass may add a tie-breaker and delegate other cases to super(). Because the parent checks with isinstance, it accepts subclass instances as well.

inheritance.py
class Animal:
    def __init__(self, name, weight):
        self.name = name
        self.weight = weight

    def __gt__(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 __gt__(self, other):
        if not isinstance(other, Dog):
            return super().__gt__(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)
print(a > d2)

Two dogs of equal weight are ordered by name. When the right operand is a plain Animal, Dog.__gt__ delegates to the parent and weight decides; the last line shows the reverse case returning False. Using one shared field for cross-class comparisons keeps the relation predictable.

$ python inheritance.py
True
False
True
False

Reducing boilerplate with total_ordering

With __gt__ and __eq__ defined, functools.total_ordering supplies the remaining comparison methods. Both hooks return NotImplemented for other types so the reflected side gets a chance.

total_ordering.py
from functools import total_ordering


@total_ordering
class Score:
    def __init__(self, points):
        self.points = points

    def __gt__(self, other):
        if not isinstance(other, Score):
            return NotImplemented
        return self.points > other.points

    def __eq__(self, other):
        if not isinstance(other, Score):
            return NotImplemented
        return self.points == other.points


s1 = Score(85)
s2 = Score(90)
print(s1 > s2)
print(s1 < s2)
print(s1 <= s2)
print(s1 == s2)

Although the class defines only two comparisons, <, <=, and equality all work. The generated methods are derived from > and ==, so a consistent pair is enough to give the class a full ordering.

$ python total_ordering.py
False
True
True
False

Best practices

Comparisons should be symmetric and unsurprising:

Source

object.__gt__ - Python documentation

Emulating numeric types - Python documentation

functools.total_ordering - Python documentation

In this article we have implemented the __gt__ method. We have covered the > dispatch, mixed types, the reflected __lt__ hook, inheritance, and total_ordering. For related topics, see Python __lt__ method, Python __ge__ method, and Python __le__ method.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Python tutorials.