A Banking Class Example#

Following the tutorials in Educative I wrote my first class with inheritance. It’s basic but it works.

It’s a far cry from what I’ve done previously in C# and the self takes some getting used to.

I’m running a pep8 (flake8) plugin in my Vim to check my code, but there is a fair bit of confusion happening with my autocomplete. It’s messing with my head, particularily here in Goyo editing mode.

Here’s the class I’ve written, admist all this confusion.

class Account:
    def __init__(self, title=None, balance=0):
        self.title = title
        self.balance = balance

    def withdrawal(self, amount):
        self.balance -= amount

    def deposit(self, amount):
        self.balance += amount

    def getBalance(self):
        print(self.balance)


class SavingsAccount(Account):
    def __init__(self, title=None, balance=0, interestRate=0):
        # calling the constructor from the parent class
        super().__init__(title, balance)
        self.interestRate = interestRate

    def interestAmount(self):
        print(self.balance * self.interestRate / 100)


a = SavingsAccount("Mark", 5000, 5)
a.deposit(1000)
a.withdrawal(500)
a.getBalance()
a.interestAmount()

I’m chuffed to be able to do it through Vim and run it in a terminal below the code. Similar experience to VS Code? Kind of but there’s a feeling of knowing the how and a little of the why.

Okay, on to Polymorphism.