class: list # welcome to discussion 7! Announcements (starting berkeley time!): - Project 3 (Ants) due **tonight**. - Homework 5 due Tuesday, 3/17. - Cats Composition due Thurs, 4/2. - Midterm next Thursday, 3/19. - Feedback **esp. on this online format!!**: **links.cs61a.org/addison-feedback** Schedule: - OOP - WWPD - Email - Inheritance - Cat - NoisyCat/Su13 Final - Linked Lists - Sum Nums - Multiply Links --- class: list # OOP - Key idea: **Treating Data As Objects**. - Remember this? ```python def student(name, year): return [name, year] def get_name(student): return student[0] ``` -- - Now, with classes, we can write this: ```python class Student: # class school = "UC Berkeley" # class attribute def __init__(self, name, year): # method self.name = name # instance attribute self.year = year # instance attribute ``` ```python >>> addison = Student("add", 2022) # instance ``` - Quick Demo! --- class: title # WWPD --- class: title # Email --- ```python class Email: def __init__(self, msg, sender_name, recipient_name): self.msg = msg self.sender_name = sender_name self.recipient_name = recipient_name class Server: def __init__(self): self.clients = {} def send(self, email): client = self.clients[email.recipient_name] client.receive(email) def register_client(self, client, client_name): self.clients[client_name] = client class Client: def __init__(self, server, name): self.inbox = [] self.server = server self.name = name self.server.register_client(self, self.name) def compose(self, msg, recipient_name): email = Email(msg, self.name, recipient_name) self.server.send(email) def receive(self, email): self.inbox.append(email) ``` --- # Inheritance ```python class Dog(): def __init__(self, name, owner): self.is_alive = True self.name = name self.owner = owner def eat(self, thing): print(self.name + " ate a " + str(thing) + "!") def talk(self): print(self.name + " says woof!") class Cat(): def __init__(self, name, owner, lives=9): self.is_alive = True self.name = name self.owner = owner self.lives = lives def eat(self, thing): print(self.name + " ate a " + str(thing) + "!") def talk(self): print(self.name + " says meow!") ``` Seems like ... a lot of overlapping code! _:thinking:_ --- # Inheritance ```python class Pet(): def __init__(self, name, owner): self.is_alive = True # It's alive!!! self.name = name self.owner = owner def eat(self, thing): print(self.name + " ate a " + str(thing) + "!") def talk(self): print(self.name) class Dog(Pet): def talk(self): print(self.name + ' says woof!') ``` --- # Cat -- ```python class Cat(Pet): def __init__(self, name, owner, lives=9): Pet.__init__(self, name, owner) ## or below! ## super().__init__(name, owner) self.lives = lives def talk(self): print(self.name + ' says meow!') def lose_life(self): if self.lives > 0: self.lives -= 1 if self.lives == 0: self.is_alive = False else: print("This cat has no more lives to lose :(") ``` --- # NoisyCat -- ```python class NoisyCat(Cat): # inherits from Cat! def __init__(self, name, owner, lives=9): Cat.__init__(self, name, owner, lives) def talk(self): Cat.talk(self) Cat.talk(self) ``` --- # NoisyCat, a second look Why doesn't this work? ```python class NoisyCat(Cat): # inherits from Cat! def __init__(self, name, owner, lives=9): Cat.__init__(self, name, owner, lives) def talk(self): self.talk() self.talk() ``` --- # Final NoisyCat ```python class NoisyCat(Cat): # inherits from Cat! def talk(self): Cat.talk(self) Cat.talk(self) ``` --- # Linked Lists - Link Constructor: ```python one_item = Link(1) one_item = Link(1, Link.empty) # these two lines are the same! two_items = Link(1, Link(2)) ``` - Item selection: ```python >>> one_item.first 1 >>> one_item.rest () >>> two_items.first 1 >>> two_items.rest Link(2) ``` --- # Sum Nums ```python def sum_nums(lnk): if lnk == Link.empty: return 0 return lnk.first + sum_nums(lnk.rest) ``` --- # Multiply Links ```python def multiply_lnks(lst_of_lnks): product = 1 for lnk in lst_of_lnks: if lnk is Link.empty: return Link.empty product *= lnk.first lnk_of_lnks_rests = [lnk.rest for lnk in lst_of_lnks] return Link(product, multiply_lnks(lnk_of_lnks_rests)) ```