""" This file includes the most basic implementation of the Account class from the book, and also an alternative implementation using a dictionary to hold the account information. Both versions work fine for this simple case, but the class implementation packs the data (account info) and the corresponding functions more closely together. This improves structure and readability, which may be important for larger software systems. """ class Account: def __init__(self, name, account_number, initial_amount): self.name = name self.no = account_number self.balance = initial_amount def deposit(self, amount): self.balance += amount def withdraw(self, amount): self.balance -= amount def dump(self): s = '%s, %s, balance: %s' % \ (self.name, self.no, self.balance) print(s) #This could also have been solved with dictionaries: def make_account(name, account_number, initial_amount): return {"name":name,"no":account_number,"balance":initial_amount} def deposit(account, amount): account["balance"] += amount def withdraw(account, amount): account["balance"] -= amount def dump(account): s = '%s, %s, balance: %s' % \ (account["name"], account["no"], account["balance"]) print(s) #this code uses the class implementation to make #two objects a1, a2 of the class Account a1 = Account('John Olsson', '19371554951', 20000) a2 = Account('Liz Olsson', '19371564761', 20000) a1.deposit(1000) a1.withdraw(4000) a2.withdraw(10500) a1.withdraw(3500) a1.dump() a2.dump() #and this code uses the dictionary version to make #a single dictionary d1, which contains the same account #information as a1 d1 = make_account('John Olsson', '19371554951', 20000) deposit(a1,1000) dump(d1)