oreoblackberry.blogg.se

Python oop inheritance
Python oop inheritance













python oop inheritance

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.

python oop inheritance

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

  • When adding parameters to _init_(), remember that parameters without default values should be placed before parameters that have default values.
  • The _eq_() method should accept two arguments, usually called self and other.
  • Examine the print statements and the output in the console.
  • Define an _eq_() method that returns True if the number attribute of two objects is equal.
  • python oop inheritance

    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















    Python oop inheritance