Get startedGet started for free

Operator overloading: comparing objects

1. Operator overloading: comparing objects

Welcome back. In this chapter, we'll learn how to compare objects.

2. Object equality

As a class is a blueprint, we might expect two objects with identical attributes to be treated as equals, right? If we check that these two objects are equal, the result is "False". This makes sense: we can have two different customers with the same name and account balance.

3. Object equality

But what if each customer has a unique account ID? Then, two customers with the same ID should be treated as equal... but they aren't. Why? Python doesn't consider two objects with the same data equal by default because of how the objects and variables representing them are stored.

4. Variables are references

If we try to print the value of a customer object, we'll see "Customer at" and a series of characters. When an object is created, Python allocates a chunk of memory to that object, and the variable the object is assigned to just contains the reference to the memory chunk. These characters represent the memory location. So, this code tells Python to allocate a chunk of memory for a customer object and label it customer_one, then allocate another chunk for customer_two. When we compare customer_one and customer_two, we are comparing references, not the data. Because the two objects point to different chunks in memory, they are not considered equal.

5. Custom comparison

But it doesn't have to be that way! For example, lists are compared using their data. Here, we initialize two lists with the same data, and Python considers them equal. Same with other data structures such as sets. So, how can we enforce this comparison using data, or attributes, for our custom classes?

6. The __eq__() method

We can define a special method for this. We know about the __init__ constructor, which is implicitly called when an object is created. Similarly, there is the __eq__ method, which Python implicitly calls whenever two objects of the same class are compared. The method should accept two arguments, referring to the objects to be compared. They are usually called self and other by convention. It should always return a Boolean value - True or False.

7. The __eq__() method

Here, we have a Customer class with id and name attributes, and we define our custom eq method passing self and other. We customize the return statement which, by default, looks at whether the two objects are allocated to the same memory chunk. It will now compare the id and name of the two objects, returning True if the values of these attributes are equal. We also included a printout for illustration.

8. Comparison of objects

Now, if we create two objects containing the same data and try to compare them using the double equality sign, our custom eq method is called, which we see from the diagnostic printout. As both objects have the same attribute values, the comparison returns "True". On the other hand, we get "False" for two objects with different id values.

9. Checking types

This is a good start, but what if we tried comparing two objects from different classes with the same attributes and values? Python will see them as equal! To avoid this, we also need to ensure that the two objects are from the same class, which we can do by checking their type within our eq method.

10. Other comparison operators

In Python, each comparison operator has an equivalent method that we can implement in a custom class, as shown in the table. When we write an exclamation mark followed by an equals sign, Python will automatically attempt to use the not equals method, or ne for short. To customize any of these, we need to define them as methods within our custom class.

11. Let's practice!

Now, let's practice defining custom equality functions!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.