Get Started

Overloading equality

When comparing two objects of a custom class using ==, Python by default compares just the object references, not the data contained in the objects. To override this behavior, the class can implement the special __eq__() method, which accepts two arguments -- the objects to be compared -- and returns True or False. This method will be implicitly called when two objects are compared.

The BankAccount class from the previous chapter is available for you in the script pane. It has one attribute, balance, and a withdraw() method. Two bank accounts with the same balance are not necessarily the same account, but a bank account usually has an account number, and two accounts with the same account number should be considered the same.

This is a part of the course

“Object-Oriented Programming in Python”

View Course

Exercise instructions

Try selecting the code in lines 1-7 and pressing the "Run code" button. Then try to create a few BankAccount objects in the console and compare them.

  • Modify the __init__() method to accept a new parameter - number - and initialize a new number attribute.
  • Define an __eq__() method that returns True if the number attribute of two objects is equal.
  • Examine the print statements and the output in the console.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

class BankAccount:
   # MODIFY to initialize a number attribute
    def __init__(self, balance=0):
        self.balance = balance
      
    def withdraw(self, amount):
        self.balance -= amount 
    
    # Define __eq__ that returns True if the number attributes are equal 
    def ____(____, ____):
        return ____.number == ____.____   

# Create accounts and compare them       
acct1 = BankAccount(123, 1000)
acct2 = BankAccount(123, 1000)
acct3 = BankAccount(456, 1000)
print(acct1 == acct2)
print(acct1 == acct3)
    
Edit and Run Code

This exercise is part of the course

Object-Oriented Programming in Python

AdvancedSkill Level
4.6+
70 reviews

Dive in and learn how to create classes and leverage inheritance and polymorphism to reuse and optimize code.

In this chapter, you'll learn how to make sure that objects that store the same data are considered equal, how to define and customize string representations of objects, and even how to create new error types. Through interactive exercises, you’ll learn how to further customize your classes to make them work more like standard Python data types.

Exercise 1: Operator overloading: comparisonExercise 2: Overloading equality
Exercise 3: Checking class equalityExercise 4: Comparison and inheritanceExercise 5: Operator overloading: string representationExercise 6: String formatting reviewExercise 7: String representation of objectsExercise 8: ExceptionsExercise 9: Catching exceptionsExercise 10: Custom exceptionsExercise 11: Handling exception hierarchies

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free