I read somewhere that __init__
stores information while creating the object. So, let's say I have this code:
class BankAccount(object):
def __init__(self, deposit):
self.amount = deposit
def withdraw(self, amount):
self.amount -= amount
def deposit(self, amount):
self.amount += amount
def balance(self):
return self.amount
myAccount = BankAccount(16.20)
x = raw_input("What would you like to do?")
if x == "deposit":
myAccount.deposit(int(float(raw_input("How much would you like to deposit?"))))
print "Total balance is: ", myAccount.balance()
elif x == "withdraw":
myAccount.withdraw(int(float(raw_input("How much would you like to withdraw?"))))
print "Total balance is: ", myAccount.balance()
else:
print "Please choose 'withdraw' or 'deposit'. Thank you."
What is __init__
doing or storing. I don't understand what "self.amount" is or how making it = deposit does anything. Is the "self.amount" under __init__
the same as the one under withdraw
? I'm just not understanding what "self_amount" does.