

What would happen if two accounts have the same account number but different balances? The code you wrote will treat these accounts as equal, but it might be better to throw an error – an exception – instead, informing the user that something is wrong. Notice that your method compares just the account numbers, but not balances.

# Define _eq_ that returns True if the number attributes are equal # MODIFY to initialize a number attribute

Modify the _init_() method to accept a new parameter – number – and initialize a new number attribute.Then try to create a few BankAccount objects in the console and compare them. Try selecting the code in lines 1-7 and pressing the “Run code” button. 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. It has one attribute, balance, and a withdraw() method. The BankAccount class from the previous chapter is available for you in the script pane. This method will be implicitly called when two objects are compared. 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. When comparing two objects of a custom class using =, Python by default compares just the object references, not the data contained in the objects. A summary from a course on : Overloading equality
