Python __ge__ Method Tutorial

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

What is __ge__ Method in Python?

The Greater Than or Equal 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 or equal comparison operation and run an accurate comparison based on the requirement of our program, Python has provided a method called __ge__.

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 or equal 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 __ge__ Method Syntax

def __ge__(self, other):

Python __ge__ 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 __ge__ 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 __ge__ method

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

    def __ge__(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 or equal to the salary of John! ")
else:
    print("The salary of Jack is less than or equal to the salary of John! ")

Output:

The salary of Jack is less than or equal to the salary of John!

In this example, we’ve overloaded the `__ge__` 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 or Equal operation (as the Left operand) the Python execution engine (aka the interpreter) will call the `__ge__` 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 `__ge__` method could be of any type! Also remember that the return value of this method could also be of anything of any type.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies