Bank Account: self.balance vs. balance?


#1

hello! have a quick question on the bank account project.

regarding the member variable balance: why do we NOT use self.balance when we initially assign the variable (we only use balance), but then later in the script use self.balance when we refer to it in every situation?

here is my code:

class BankAccount(object):
  balance = 0
  def __init__(self, name):
    self.name = name
  def __repr__(self):
    # note: __repr__ requires return, not print
    # note: we use %.2f not %s.2f
    return "account of %s with balance $%.2f" \
    % (self.name, self.balance)
  def show_balance(self):
    print "your balance is: $%.2f" % self.balance
  def deposit(self, amount):
    if amount <= 0:
        print "deposit must be greater than zero"
        return
    else:
    	print "your desposit is $%.2f" % amount
    	self.balance += amount
    	self.show_balance()
    	#show_balance is called on the self object
  def withdraw(self, amount):
    if amount > self.balance:
        print "withdrawal cannot exceed balance"
        return
    else:
        print "your withdrawal is $%.2f" % amount
        self.balance -= amount
        self.show_balance()


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.