Go Back

Python program to overload equality -Operator Overloading

8/6/2022
All Articles

Python program to overload equality

Python program to overload equality -Operator Overloading

Python program to overload equality -Operator Overloading 

Below is example to overload =  or less than operator 

class A:
    def __init__(self, a):
        self.a = a
    def __lt__(self, other):
        if(self.a<other.a):
            return "ob1 is lessthan ob2"
        else:
            return "ob2 is less than ob1"
    def __eq__(self, other):
        if(self.a == other.a):
            return "Both are equal"
        else:
            return "Not equal"
                
ob1 = A(2)
ob2 = A(3)
print(ob1 < ob2)

ob3 = A(4)
ob4 = A(4)
print(ob1 == ob2)
 

Article