Question on classes / methods


#1
class BankAccount(object):
  balance = 0
  def __init__(self, name):
    self.name = name
  def __repr__(self):
    return "%s's account. Balance $%.2f" % (self.name, self.balance) 
  def show_balance(self):
    print "%s's account. Balance $%.2f" % (self.name, self.balance)
  def deposit(self, amount):
    if amount <= 0:
      print "Error can not despoit 0 dollars"
      return
    else:
      print "You are despoiting: $%.2f" % amount
      self.balance += amount
      self.show_balance()
  def withdraw(self, amount):
    if amount >= self.balance:
      print "Can not withdraw more than your balance!"
    else:
      print "You are withdrawing: $%.2f" % amount
      self.balance -= amount
      self.show_balance()
      
      
my_account = BankAccount("bob")
my_account.show_balance()
my_account.deposit(2000)
my_account.withdraw(1000)

So I have a few questions.

Question 1:

def __repr__(self):
    return "%s's account. Balance $%.2f" % (self.name, self.balance)

Why do you need to write self.balance as such instead of just balance? in def __repr__(self):, I didn’t pass balance into it so why does it need to be self? Is it because of the variable defined in class BankAccount is a member variable of the entire class?

Question 2:
Why do you call everything as my_variable.class(parameter/object) like:

my_account = BankAccount("bob")
my_account.show_balance()
my_account.deposit(2000)
my_account.withdraw(1000)

Question 3:
Why is print "You are despoiting: $%.2f" % amount not print "You are despoiting: $%.2f" % (self.amount) like print "%s's account. Balance $%.2f" % (self.name, self.balance)?


#2

Q1: You put self in front of each variable because those are dependent upon what self is. Basically, the self parameter is a placeholder for the name of a BankAccount object. Each BankAccount object has its own member variables, so using any using self.randomvariable is necessary. Balance is not defined anywhere on its own. The value of balance is contigent on whatever self is. After all, bob.balance and steve.balance could have different values.
Q2: Each of those functions: .showbalance(), .deposit(), and .withdraw() are functions that are only available to BankAccount objects. Also, it’s similar to using .upper() and .lower() on strings, we don’t do upper(my_string) or lower(my_string).
Q3: When you call my_account.deposit(amount) and my_account.withdraw(amount), amount is not saved as a member variable of my_account. The amount is a parameter that represents a value that will you place into the function and reference as the function runs (but you will not need to keep this value after the function has done its job. Basically, in this program, two values are saved to every BankAccount object: name and balance. Those are the only two values that can be accessed; however, these values can be changed by calling the functions my_account.deposit(amount) and my_account.withdraw(amount) with a certain parameter amount. After the account is updated, there is no need to store the exact amount transferred for later. If you want, you could record the amount by adding more member variables like lastpurchase1, lastpurchase2, etc.