1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Object-Oriented Programming in Python

Connected

Exercise

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 it compares 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.

Instructions

100 XP
  • 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.
  • Check if acct and pn are equal.