1 of 58

Dis 06:

Object Oriented Programming

TA: Anita Cu

anitacu5@berkeley.edu

2 of 58

Agenda

0 ) Administrivia

1 ) Dictionaries

2 ) OOP

3 ) Inheritance

2

3 of 58

Administrivia

  • HW 05 due tonight 3/8
  • Lab 06 due tonight 3/8
  • Ants due next Thursday 3/14
  • Midterm 2 coming up!

3

4 of 58

Dictionaries

1

5 of 58

Dictionaries

  • dictionaries: data structure that maps key to values

5

6 of 58

Dictionaries

  • dictionaries: data structure that maps key to values
  • keys must be unique and immutable types

6

7 of 58

Dictionaries

  • dictionaries: data structure that maps key to values
  • keys must be unique and immutable types
    • Strings, numbers, tuples, etc

7

8 of 58

Dictionaries

  • dictionaries: data structure that maps key to values
  • keys must be unique and immutable types
    • Strings, numbers, tuples, etc
    • NOT lists, dictionaries

8

9 of 58

Dictionaries

  • dictionaries: data structure that maps key to values
  • keys must be unique and immutable types
    • Strings, numbers, tuples, etc
    • NOT lists, dictionaries
  • keys-value pairs are not ordered in any specific order

9

10 of 58

Dictionaries

  • dictionaries: data structure that maps key to values
  • keys must be unique and immutable types
    • Strings, numbers, tuples, etc
    • NOT lists, dictionaries
  • keys-value pairs are not ordered in any specific order
  • dictionaries are mutable!

10

11 of 58

Dictionaries

  • dictionaries: data structure that maps key to values
  • keys must be unique and immutable types
    • Strings, numbers, tuples, etc
    • NOT lists, dictionaries
  • keys-value pairs are not ordered in any specific order
  • dictionaries are mutable!
    • can add, remove, modify key-value pairs

11

12 of 58

Dictionaries

  • dictionaries: data structure that maps key to values
  • keys must be unique and immutable types
    • Strings, numbers, tuples, etc
    • NOT lists, dictionaries
  • keys-value pairs are not ordered in any specific order
  • dictionaries are mutable!
    • can add, remove, modify key-value pairs
    • Q: What happens if want to insert a new value for a key that already exists?

12

13 of 58

Dictionaries

  • dictionaries: data structure that maps key to values
  • keys must be unique and immutable types
    • Strings, numbers, tuples, etc
    • NOT lists, dictionaries
  • keys-value pairs are not ordered in any specific order
  • dictionaries are mutable!
    • can add, remove, modify key-value pairs
    • Q: What happens if want to insert a new value for a key that already exists?
    • A: The old value gets overridden since all keys are unique! Cannot have two key-value pairs with the same key.

13

14 of 58

Dictionary Methods

  • d[k] = val

14

15 of 58

Dictionary Methods

  • d[k] = val
    • adds value val to corresponding key k to dictionary d, or replaces old value with new value val to existing key k

15

16 of 58

Dictionary Methods

  • d[k] = val
    • adds value val to corresponding key k to dictionary d, or replaces old value with new value val to existing key k

  • del d[k]

16

17 of 58

Dictionary Methods

  • d[k] = val
    • adds value val to corresponding key k to dictionary d, or replaces old value with new value val to existing key k

  • del d[k]
    • removes dictionary value associated with key k

17

18 of 58

Dictionary Methods

  • d[k] = val
    • adds value val to corresponding key k to dictionary d, or replaces old value with new value val to existing key k

  • del d[k]
    • removes dictionary value associated with key k

  • d.pop(k)

18

19 of 58

Dictionary Methods

  • d[k] = val
    • adds value val to corresponding key k to dictionary d, or replaces old value with new value val to existing key k

  • del d[k]
    • removes dictionary value associated with key k

  • d.pop(k)
    • removes key-value pair with key k, and returns the associated value

19

20 of 58

Iterating Through Contents of

a Dictionary

  • 2 ways to iterate through a dictionary’s keys

20

21 of 58

Iterating Through Contents of

a Dictionary

  • 2 ways to iterate through a dictionary’s keys
    • for key in dictionary:
    • for key in dictionary.keys():

21

22 of 58

Iterating Through Contents of

a Dictionary

  • 2 ways to iterate through a dictionary’s keys
    • for key in dictionary:
    • for key in dictionary.keys():

  • iterate through a dictionary’s values
    • for key in dictionary.values():

22

23 of 58

Iterating Through Contents of

a Dictionary

  • 2 ways to iterate through a dictionary’s keys
    • for key in dictionary:
    • for key in dictionary.keys():

  • iterate through a dictionary’s values
    • for key in dictionary.values():

  • iterate through a dictionary’s key and values
    • for key, value in dictionary.items():

23

24 of 58

Object Oriented Programming

(OOP)

2

25 of 58

OOP Goal

  • Treat data as objects, like in real-life

25

26 of 58

OOP Terminology

  • class: a template for creating objects

26

27 of 58

OOP Terminology

  • class: a template for creating objects
    • Student, Professor

27

28 of 58

OOP Terminology

  • class: a template for creating objects
    • Student, Professor
  • instance: a single object created from a class using its constructor

28

29 of 58

OOP Terminology

  • class: a template for creating objects
    • Student, Professor
  • instance: a single object created from a class using its constructor
    • anita = Student(“Anita”, “Junior”, “CS”)

29

30 of 58

OOP Terminology

  • class: a template for creating objects
    • Student, Professor
  • instance: a single object created from a class using its constructor
    • anita = Student(“Anita”, “Junior”, “CS”)
  • instance attribute: a property of an object specific to (belongs to) an instance

30

31 of 58

OOP Terminology

  • class: a template for creating objects
    • Student, Professor
  • instance: a single object created from a class using its constructor
    • anita = Student(“Anita”, “Junior”, “CS”)
  • instance attribute: a property of an object specific to (belongs to) an instance
    • name, year, majoranita.name is “Anita”

31

32 of 58

OOP Terminology

  • class: a template for creating objects
    • Student, Professor
  • instance: a single object created from a class using its constructor
    • anita = Student(“Anita”, “Junior”, “CS”)
  • instance attribute: a property of an object specific to (belongs to) an instance
    • name, year, majoranita.name is “Anita”
  • class attribute: a property of an object that is shared by all instances of the same class

32

33 of 58

OOP Terminology

  • class: a template for creating objects
    • Student, Professor
  • instance: a single object created from a class using its constructor
    • anita = Student(“Anita”, “Junior”, “CS”)
  • instance attribute: a property of an object specific to (belongs to) an instance
    • name, year, majoranita.name is “Anita”
  • class attribute: a property of an object that is shared by all instances of the same class
    • Student.professor → dan_garcia

33

34 of 58

OOP Terminology

  • class: a template for creating objects
    • Student, Professor
  • instance: a single object created from a class using its constructor
    • anita = Student(“Anita”, “Junior”, “CS”)
  • instance attribute: a property of an object specific to (belongs to) an instance
    • name, year, majoranita.name is “Anita”
  • class attribute: a property of an object that is shared by all instances of the same class
    • Student.professor → dan_garcia
  • method: a function/action that all instances of a class can perform

34

35 of 58

OOP Terminology

  • class: a template for creating objects
    • Student, Professor
  • instance: a single object created from a class using its constructor
    • anita = Student(“Anita”, “Junior”, “CS”)
  • instance attribute: a property of an object specific to (belongs to) an instance
    • name, year, majoranita.name is “Anita”
  • class attribute: a property of an object that is shared by all instances of the same class
    • Student.professor → dan_garcia
  • method: a function/action that all instances of a class can perform
    • watchLecture(), doHomework(), takeExam()

35

36 of 58

Method vs. Function

  • method: function encapsulated by a class

36

37 of 58

Method vs. Function

  • method: function encapsulated by a class
  • function: routine defined outside of a class

37

38 of 58

Important Notes!

  • The instance specified is passed in implicitly as self

38

39 of 58

Mini Quiz!

class Student:

def __init__(self, name):

self.name = name

def attend_lecture(self, subject):

print(self.name + “ is learning about “ + subject)

anita = Student(“Anita”)

39

40 of 58

Mini Quiz!

Q: Which of these are valid method calls?

> anita.attend_lecture(“OOP”)

> anita.attend_lecture(anita, “OOP”)

> Student.attend_lecture(“OOP”)

> Student.attend_lecture(anita, “OOP”)

40

41 of 58

Mini Quiz!

Q: Which of these are valid method calls?

> anita.attend_lecture(“OOP”)

> anita.attend_lecture(anita, “OOP”)

> Student.attend_lecture(“OOP”)

> Student.attend_lecture(anita, “OOP”)

41

42 of 58

Mini Quiz!

Q: Which of these are valid method calls?

> anita.attend_lecture(“OOP”)

> anita.attend_lecture(anita, “OOP”)

attend_lecture takes 2 arguments, but this call passes in 3 arguments! What are the 3 arguments?

> Student.attend_lecture(“OOP”)

> Student.attend_lecture(anita, “OOP”)

42

43 of 58

Mini Quiz!

Q: Which of these are valid method calls?

> anita.attend_lecture(“OOP”)

> anita.attend_lecture(anita, “OOP”)

attend_lecture takes 2 arguments, but this call passes in 3 arguments! What are the 3 arguments?

> Student.attend_lecture(“OOP”)

No specific instance for this method was given.

> Student.attend_lecture(anita, “OOP”)

43

44 of 58

Mini Quiz!

Q: Which of these are valid method calls?

> anita.attend_lecture(“OOP”)

> anita.attend_lecture(anita, “OOP”)

attend_lecture takes 2 arguments, but this call passes in 3 arguments! What are the 3 arguments?

> Student.attend_lecture(“OOP”)

No specific instance for this method was given.

> Student.attend_lecture(anita, “OOP”)

44

45 of 58

Inheritance

2

Let’s reuse even more code!

46 of 58

Current Implementation

class Dog(object):

def __init__(self, name, owner):

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!”)

46

47 of 58

Current Implementation

class Dog(object):

def __init__(self, name, owner):

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(object):

def __init__(self, name, owner, lives=9):

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!”)

47

48 of 58

Current Implementation

class Dog(object):

def __init__(self, name, owner):

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(object):

def __init__(self, name, owner, lives=9):

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!”)

48

Any problems with this?

49 of 58

Current Implementation

class Dog(object):

def __init__(self, name, owner):

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(object):

def __init__(self, name, owner, lives=9):

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!”)

49

So much repeated code! :(

50 of 58

Current Implementation

class Dog(object):

def __init__(self, name, owner):

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(object):

def __init__(self, name, owner, lives=9):

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!”)

50

So much repeated code! :(

51 of 58

Let’s use inheritance!

51

Consolidate the similar code into one parent class that generalizes both subclasses!

52 of 58

Let’s use inheritance!

class Pet(object):

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)

52

Consolidate the similar code into one parent class that generalizes both subclasses!

53 of 58

Let’s use inheritance!

class Pet(object):

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)

53

Now we can have the Dog class extend the Pet class to inherit all of the instance variables and methods!

Syntax to do so:

class subclass(superclass)

54 of 58

Let’s use inheritance!

class Pet(object):

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)

class Dog(Pet):

54

Now we can have the Dog class extend the Pet class to inherit all of the instance variables and methods!

Syntax to do so:

class subclass(superclass)

55 of 58

Let’s use inheritance!

class Pet(object):

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)

class Dog(Pet):

55

If want a subclass to behave differently than parent class, override the method with a more specific implementation!

56 of 58

Let’s use inheritance!

class Pet(object):

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)

class Dog(Pet):

def talk(self):

print(self.name + “ says Woof!”)

56

If want a subclass to behave differently than parent class, override the method with a more specific implementation!

57 of 58

Inheritance Hierarchy

57

Animal

Dog

Cat

Poodle

58 of 58

Inheritance Hierarchy

58

Animal

Dog

Cat

Poodle

class Poodle(Dog)

MUST follow an IS-A relationship

  • a Poodle is a Dog