Python __gt__ Method Tutorial

In this section, we will learn what the __gt__ method is and how to use it in Python.

What is __gt__ Method in Python?

The Greater Than operator `>` by default is capable of taking basic data types like `str`, `int`, `float` etc. as its operands and return whether they are equal in value.

But if the values involved in the operation were of custom data types (For example, a custom class that we’ve created), then with a high chance the comparison will result inaccurate! Because python doesn’t know based on the value of what attribute, it should run the comparison!

Now in order to make a custom object get involved in a greater-than comparison operation and run an accurate comparison based on the requirement of our program, Python has provided a method called __gt__.

If we override this method in a class and create an object from it, then the python execution engine will call this method automatically when the target object gets involved in a greater-than comparison operation. Note that the target object should be declared as the left-hand operator so that Python can call this method! This means if the object is the right-hand operand in the comparison, then the method won’t get called.

Python __gt__ Method Syntax

def __gt__(self, other):

Python __gt__ Method Parameters

The method has two parameters and they are filled automatically by Python interpreter behind the scene when the target object is involved in the comparison operation.

  • The first parameter of this method is a reference to the object that is involved in a comparison operation (the one that is set as the left-hand operand in the comparison).
  • The second parameter is a reference to the right-hand operand involved in the comparison operation.

Python __gt__ Method Return Value

The return value of this method typically should be a boolean value! But there’s no obligation and so the return value could be of any data type!

Example: using python __gt__ method

class Employee: 
    
    def __init__(self, age, salary):
        self.age = age
        self.salary = salary

    def __gt__(self,other):
        return self.salary > other.salary 


jack = Employee(40, 200000)
john = Employee(60, 300000)

if jack > john:
    print("The salary of Jack is greater than the salary of John! ")
else:
    print("The salary of Jack is less than the salary of John! ")

Output:

The salary of Jack is less than the salary of John!

In this example, we’ve overloaded the `__gt__` method in the `Employee` class. We then use the value of the `salary` attribute as the measurement of comparison within the body of this method.

So when an object of type Employee gets involved in a Greater Than operation (as the Left operand) the Python execution engine (aka the interpreter) will call the `__gt__` method and passes the arguments and runs the body of the method to get the final value.

Note that this example considered the right operand involved in the comparison operation as an object of type Employee, but this is not an obligation and it’s just a design decision! Basically, based on your requirements for a class, the `other` parameter of the `__gt__` method could be of any type! Also remember that the return value of this method could also be any type.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies