In this section, we will learn what the __ne__ method is and how to use it in Python.
What is __ne__ Method in Python?
The not equal operator `!=` by default is capable of taking basic data types like `str`, `int`, `float` etc. as its operands and returns a boolean value to show whether they are not 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 not-equal comparison operation and run an accurate comparison based on the requirement of our program, Python has provided a method called __ne__.
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 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 __ne__ Method Syntax
def __ne__(self, other):
Python __ne__ 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 __ne__ 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 __ne__ method
class Employee: def __init__(self, age, salary): self.age = age self.salary = salary def __ne__(self,other): return self.salary != other.salary jack = Employee(40, 200000) john = Employee(60, 300000) if jack != john: print("These employees have different salaries!") else: print("The salary of both employees are the same. ")
These employees have different salaries!
In this example, we’ve overloaded the `__ne__` 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 not equal operation (as the Left operand) the Python execution engine (aka the interpreter) will call the `__ne__` 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 `__ne__` method could be of any type! Also remember that the return value of this method could also be of anything of any type.