1 of 12

CSE 163

Inheritance�

Suh Young Choi��🎶 Listening to: Alan Becker�💬 Before Class: Before was was was, was was is. Ruminate!

2 of 12

This Time

  • Lambda functions
  • Magic methods
  • Introducing THA 4

Last Time

  • Private fields and methods
  • Default parameters

2

3 of 12

Announcements

Checkpoint 3 due next Monday!

THA 4: Networks due next Thursday! (Please start early ☺ )

Project EDA + Portfolio Milestone released Monday

Section 5 extended to 11:59pm today

3

4 of 12

Project EDA

Exploratory Data Analysis

Provide update to your Proposal

Answer some guided questions about each dataset that you are working with

Slightly adjusted requirements if you are pursuing the Multiple Datasets or Result Validity challenge goals

Submit a 3-5 page report (soft upper limit of 8 pages) + the code you have used to answer the questions + testing code

4

5 of 12

Portfolio Milestone

Polish work for THA 1, 2, and 3

Provide update to your Vision Statement

Revise and reflect on your chosen Creative Components

Slightly adjusted requirements if you are pursuing the Narrative Portfolio or Technical Deep Dive challenge goals

Submit a 3-5 page writeup (soft upper limit of 8 pages) + the code you have revised from your THAs

5

6 of 12

Lambdas

  • It’s kind of annoying to have to define a whole function just for this. It would be nice if there was a shorthand syntax

Introduce: Lambda functions– these let us define functions within the body of our code!

6

def get_dog_name(d):

return d.get_name()

sorted(dogs, key=get_dog_name)

sorted(dogs, key=lambda d: d.get_name())

7 of 12

Main Method Pattern

  • Why have we been making you do this annoying pattern?

  • If you don’t, it will run the main method if you import the file!
    • Usually not fun to run a 2 hour data analysis if you just wanted to import one helper function.

7

def main():

print('Hello world')

if __name__ == '__main__':

main()

8 of 12

Special Methods

  • The fact that you can use the same syntax for many classes is because of these “special methods”

8

Syntax

Method

x < y

x.__lt__(y)

x > y

x.__gt__(y)

x == y

x.__eq__(y)

x <= y

x.__le__(y)

x >= y

x.__ge__(y)

print(x)

print(x.__str__())

x[i]

x.__getitem__(i)

x[i] = v

x.__setitem__(i, v)

9 of 12

Inheritance

9

class Dog:

  def __init__(self, name: str) -> None:

    self._name: str = name

  def bark(self) -> None:

    print(self._name + ': Woof')

class ServiceDog(Dog):

  def __init__(self, name: str, service: str) -> None:

super().__init__(name)

    self._service: str = service

  def bark(self) -> None:

    print(self._name + ‘: *alert bark*')

10 of 12

Graphs

  • Nodes + Edges
  • In a social network, nodes are people!

10

  • B’s friends: A, D, E
  • C’s friends: A, D
  • B + C’s mutual friends: A, D

11 of 12

THA 4 Tips

  • Start early ☺

  • graphs.py
    • Edges and nodes are in a list for convenience

  • analysis.py
    • Read through the starter code thoroughly!
    • Consider what different data structures represent
    • The two recommendation algorithms will be fairly similar

  • Creative Component
    • Refer to the (node, dict) syntax for adding node attributes

11

12 of 12

Before Next Time

  • Complete Lesson 13
    • Remember not for points, but do go towards Weekly Tokens
  • Complete Checkpoint 3
  • Keep working on THA 4

Next Time

  • Statistical Testing

12