To recap, the not equal operator is applied between two types supporting comparison. Today you learned how to use the not equal operator in Python to check if a value is not equal to another. To learn more about operator overloading, check out this article. # Let's create two fruits with the same mass but with different names.Ĭustomizing or overriding a special method in Python is called operator overloading. # In this case, it only cares about the weights of the objects. # When two fruit objects are compared with !=, this method is called. # Initialize a fruit with name and weight Here is an example of a Fruit class that has a customized way to compare if two objects are not equal by comparing the weights of the fruits: class Fruit: Now, let’s take a look at an example where we write a custom _ne_() operator into a custom class To keep it short, we are not going to implement all of these methods in this guide. Here is a list of all the special methods you need to implement to support object comparison: This is because it can be confusing if your objects for example support obj1 != obj2, but not obj1 = obj2. This is to support the not-equal operator between custom objects.įor example, if you have a Fruit class, you can decide what happens when you call fruit1 != fruit2 by implementing the _ne_() method.īefore customizing the _ne_() method, it is important to notice that usually, you want to override all the other comparison methods too. This means you can provide an implementation of the _ne_() method in your class. In Python, you can override special methods. This verifies that the int class implements _ne_() method to support not the equal operator on integer objects. This is possible using the dir() function: > dir(int)Īs you can see from the ocean of attributes, one of them is called ‘_ne_’. So these two produce an identical result.Īlternatively, you can verify that the int type really implements _ne_() by checking the attributes of the int class. In this piece of code, the first call automatically gets turned into the second call. To verify this, you can check the results of the following calls: > 1 != 2 This special method defines what happens when you call not equal on two objects.įor example, comparing two integers with != is possible because Python’s int type implements the _ne_() method behind the scenes. When you call not equal ( !=) on two numbers in Python, you are calling a special method called _ne_() under the hood. The _ne_() Method in Python This code is equivalent to 1 != 2. So far you have only seen what the not-equal operator call looks like and what it returns as a result.īut as it turns out, there is a special method called _ne_() behind the scenes that makes not equal comparisons possible. Advanced Comparison with Not Equal in Python Finally, let’s take a look at an advanced topic about supporting not equal operators with custom objects.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |