# OOP class Account(object): """A bank account that allows deposits and withdrawals. >>> john = Account('John') >>> jack = Account('Jack') >>> john.deposit(10) 10 >>> john.deposit(5) 15 >>> john.interest 0.02 >>> jack.deposit(7) 7 >>> jack.deposit(5) 12 """ interest = 0.02 def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount): """Increase the account balance by amount and return the new balance.""" self.balance = self.balance + amount return self.balance def withdraw(self, amount): """Decrease the account balance by amount and return the new balance.""" if amount > self.balance: return 'Insufficient funds' self.balance = self.balance - amount return self.balance # Currying to get a bound method from operator import add def curry(f): def outer(x): def inner(*args): return f(x, *args) return inner return outer add3 = curry(add)(3) add3(4) tom_account = Account('Tom') tom_deposit = curry(Account.deposit)(tom_account) tom_deposit(1000) # Inheritance class CheckingAccount(Account): """A bank account that charges for withdrawals. >>> ch = CheckingAccount('Jack') >>> ch.balance = 20 >>> ch.withdraw(5) 14 >>> ch.interest 0.01 """ withdraw_fee = 1 interest = 0.01 def withdraw(self, amount): fee = self.withdraw_fee return Account.withdraw(self, amount+fee) class SavingsAccount(Account): """A bank account that charges for deposits.""" deposit_fee = 2 def deposit(self, amount): return Account.deposit(self, amount - self.deposit_fee) class AsSeenOnTVAccount(CheckingAccount, SavingsAccount): """A bank account that charges for everything.""" def __init__(self, account_holder): self.holder = account_holder self.balance = 1 # A free dollar! supers = [c.__name__ for c in AsSeenOnTVAccount.mro()]