diff --git a/assets/slides/14-Inheritance.pdf b/assets/slides/14-Inheritance.pdf new file mode 100644 index 000000000..3c41ee8fa Binary files /dev/null and b/assets/slides/14-Inheritance.pdf differ diff --git a/assets/slides/14.py b/assets/slides/14.py new file mode 100644 index 000000000..8b77c760c --- /dev/null +++ b/assets/slides/14.py @@ -0,0 +1,97 @@ +# Tracking Instances + +class Transaction: + """A logged transaction. + + >>> s = [20, -3, -4] + >>> ts = [Transaction(x) for x in s] + >>> ts[1].balance() + 17 + >>> ts[2].balance() + 13 + """ + log = [] + + def __init__(self, amount): + self.amount = amount + self.prior = list(self.log) + self.log.append(self) + + def balance(self): + return self.amount + sum([t.amount for t in self.prior]) + +class Account: + """An account has a balance and a holder. + + >>> a = Account('John') + >>> a.holder + 'John' + >>> a.deposit(100) + 100 + >>> a.withdraw(90) + 10 + >>> a.withdraw(90) + 'Insufficient funds' + >>> a.balance + 10 + >>> a.interest + 0.02 + """ + + interest = 0.02 # A class attribute + + def __init__(self, account_holder): + self.holder = account_holder + self.balance = 0 + + def deposit(self, amount): + """Add amount to balance.""" + self.balance = self.balance + amount + return self.balance + + def withdraw(self, amount): + """Subtract amount from balance if funds are available.""" + if amount > self.balance: + return 'Insufficient funds' + self.balance = self.balance - amount + return self.balance + + +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): + return Account.withdraw(self, amount + self.withdraw_fee) + # Alternatively: + return super().withdraw(amount + self.withdraw_fee) + +# Multiple Inheritance + +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()] diff --git a/assets/slides/15-Linked_Lists.pdf b/assets/slides/15-Linked_Lists.pdf new file mode 100644 index 000000000..759bc021c Binary files /dev/null and b/assets/slides/15-Linked_Lists.pdf differ diff --git a/assets/slides/15.py b/assets/slides/15.py new file mode 100644 index 000000000..b02e1a75c --- /dev/null +++ b/assets/slides/15.py @@ -0,0 +1,64 @@ +class Link: + """A linked list. + + >>> s = Link(3, Link(4, Link(5))) + >>> s + Link(3, Link(4, Link(5))) + >>> print(s) + <3 4 5> + >>> s.first + 3 + >>> s.rest + Link(4, Link(5)) + >>> s.rest.first + 4 + >>> s.rest.first = 7 + >>> s + Link(3, Link(7, Link(5))) + >>> s.first = 6 + >>> s.rest.rest = Link.empty + >>> s + Link(6, Link(7)) + >>> print(s) + <6 7> + >>> print(s.rest) + <7> + >>> t = Link(1, Link(Link(2, Link(3)), Link(4))) + >>> t + Link(1, Link(Link(2, Link(3)), Link(4))) + >>> print(t) + <1 <2 3> 4> + """ + empty = () + + def __init__(self, first, rest=empty): + assert rest is Link.empty or isinstance(rest, Link) + self.first = first + self.rest = rest + #Not in scope! + def __repr__(self): + if self.rest: + rest_repr = ', ' + repr(self.rest) + else: + rest_repr = '' + return 'Link(' + repr(self.first) + rest_repr + ')' + #Not in scope! + def __str__(self): + string = '<' + while self.rest is not Link.empty: + string += str(self.first) + ' ' + self = self.rest + return string + str(self.first) + '>' + +def map_link(f, s): + """Return a linked list of f(x) for each x in s. + + >>> evens = Link(4, Link(2, Link(6))) + >>> map_link(lambda x: x + 1, evens) + Link(5, Link(3, Link(7))) + >>> evens + Link(4, Link(2, Link(6))) + """ + if s is Link.empty: + return s + return Link(f(s.first), map_link(f, s.rest)) diff --git a/index.html b/index.html index 1666a8a9e..8de984905 100644 --- a/index.html +++ b/index.html @@ -1071,6 +1071,8 @@

Calendar

@@ -1133,6 +1135,8 @@

Calendar

@@ -1142,10 +1146,10 @@

Calendar

-Disc 08: Linked Lists +Disc 08: Linked Lists @@ -1571,7 +1575,7 @@

Policies

}); - let build_time = new Date(1000 * 1729541054.131282); + let build_time = new Date(1000 * 1729545120.543286);