Python __le__ Method Tutorial

  • Post author:
  • Post category:Python

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

What is __le__ Method in Python?

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

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 less 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 __le__ Method Syntax

def __le__(self, other):

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

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

    def __le__(self,other):
        return self.salary <= other.salary 


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

if jack <= john:
    print("The salary of Jack is either less or equal to the salary of John! ")
else:
    print("The salary of Jack is either greater or equal to the salary of John!")

Output:

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

In this example, we’ve overloaded the `__le__` 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 Less Than or Equal operation (as the Left operand) the Python execution engine (aka the interpreter) will call the `__le__` 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, it’s just a design decision! Basically, based on your requirements for a class, the `other` parameter of the `__le__` method could be of any type! Also remember that the return value of this method could also be of anything of any type.

Leave a Reply