The __gt__() method is part of the comparison magic methods in Python. It is used to implement the internal workings of the greater than (>) operator on objects of a given class. This means that when we call the operator on objects i.e x > Y , Python literally calls the __gt__() method on the object x, with the argument y i.e x.__gt__(y).

Most builtin data types have this method defined by default , this makes it possible to use the > operator to tell whether one value is greater than the other.

ExampleEdit & Run
print(int(5).__gt__(2))#Same as print(1 < 2)

print((1, 2).__gt__((3, 4))) # Same as print((1, 2) > (3, 4))

print([5, 2].__gt__([5, 1])) # Same as print([5, 2] > [5, 1])
Output:
TrueFalseTrue[Finished in 0.010607051895931363s]

One common application of the the __gt__() method( and consequently the > operator) is in sorting.  This is because the method evaluates logically whether one value  is greater than the other thus establishing order in the items. 

Defining the __gt__() method for custom objects

In order for a user-defined objects to be able to use the greater than operator( > ), it must define the __gt__() method.

Syntax:
class MyClass:
   def __gt__(self, other):
       #statements
self This is a compulsory parameter for all instance methods. It references the current instance of the class.
other The other object to be compared with the current instance(self)

The method should define only two required method, i.e self and other. It should return a boolean value indicating whether self is greater than other. True if so and False otherwise.

ExampleEdit & Run
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    #Define the __gt__() method
    def __gt__(self, other):
        if isinstance(other, Point):
           return (self.x, self.y) > (other.x, other.y)

        return False

p1 = Point(4, 5)
p2 = Point(3, 4)
p3 = Point(1, 2)

print(p1 > p2)
print(p3 > p1)
Output:
TrueFalse[Finished in 0.010174717986956239s]

In some cases, it is essential to check explicitly whether the "other" object is actually an instance of the current class. This ensures that we avoid incorrect  and unintentional result in the case where the two classes shares attributes with same names. This can be achieved using the builtin isinstance() method as in above example.

Another Example:

ExampleEdit & Run
class Date:
    def __init__(self, year, month, day):
         self.year = year 
         self.month = month 
         self.day = day 

    #Define the  __gt__() method
    def __gt__(self, other):
         
        if self.year > other.year:
            return True
        elif self.year == other.year: 
            if self.month > other.month:
                return True
            elif self.month == other.month:
                return self.day > other.day 

        return False

date1 = Date(2022, 12, 31)
date2 = Date(2020, 1, 1) 
date3 = Date(2023, 8, 2)

print(date1 > date2)
print(date1 > date3)
Output:
TrueFalse[Finished in 0.010322836926206946s]