Get Started

Checking class equality

In the previous exercise, you defined a BankAccount class with a number attribute that was used for comparison. But if you were to compare a BankAccount object to an object of another class that also has a number attribute, you could end up with unexpected results.

For example, consider two classes


class Phone:
  def __init__(self, number):
     self.number = number

  def __eq__(self, other):
    return self.number == \
          other.number

pn = Phone(873555333)

class BankAccount:
  def __init__(self, number):
     self.number = number

  def __eq__(self, other):
    return self.number == \
           other.number

acct = BankAccount(873555333)

Running acct == pn will return True, even though we're comparing a phone number with a bank account number.

It is good practice to check the class of objects passed to the __eq__() method to make sure the comparison makes sense.

This is a part of the course

“Object-Oriented Programming in Python”

View Course

Exercise instructions

Both the Phone and the BankAccount classes have been defined. Try running the code as-is using the "Run code" button and examine the output.

  • Modify the definition of BankAccount to only return True if the number attribute is the same and the type() of both objects passed to it is the same.

Run the code and examine the output again.

Hands-on interactive exercise

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

class BankAccount:
    def __init__(self, number, balance=0):
        self.number, self.balance = number, balance
      
    def withdraw(self, amount):
        self.balance -= amount 

    # MODIFY to add a check for the type()
    def __eq__(self, other):
        return (self.number == other.number)

acct = BankAccount(873555333)
pn = Phone(873555333)
print(acct == pn)
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 equalityExercise 3: Checking class equality
Exercise 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