Bank Account: self.balance vs. balance?


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): = 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.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"
    	print "your desposit is $%.2f" % amount
    	self.balance += amount
    	#show_balance is called on the self object
  def withdraw(self, amount):
    if amount > self.balance:
        print "withdrawal cannot exceed balance"
        print "your withdrawal is $%.2f" % amount
        self.balance -= amount


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