educative.io

Educative

Self.amount = amount

class Account:

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

    self.title = title

    self.balance = balance

def withdrawal(self, amount):

    self.amount = amount

    self.balance -= self.amount

def deposit(self, amount):

    self.amount = amount

    self.balance += self.amount

def getBalance(self):

    return self.balance

class SavingsAccount(Account):

def __init__(self, title = None, balance = 0,interestRate = 0 ):

    #super().title = title

    super().__init__(title,balance)

    self.interestRate = interestRate

def interestAmount(self):

    return (self.interestRate * self.balance/100)

It seems like I should declare
self.amount = amount
but that is not what was in the solution? Why ?

Thanks


Course: Educative: Interactive Courses for Software Developers
Lesson: Educative: Interactive Courses for Software Developers

Hi @Shawn_Adams
Thanks for reaching out to us.
self is only used when

  • Defining instance methods because it is automatically passed as the first parameter
  • The method is called when referencing a class or instance attribute from within an instance method
  • Referencing instance variables and methods from other instance methods
    We cannot use self.amount because it is neither an instance variable nor an instance method in this case. If there is a variable in this class named amount. we can utilize it.
    I hope it helps. Happy Learning :slight_smile: