CS 61A Discussion 6 Nonlocal and Object Oriented Programming Albert - - PowerPoint PPT Presentation

cs 61a discussion 6
SMART_READER_LITE
LIVE PREVIEW

CS 61A Discussion 6 Nonlocal and Object Oriented Programming Albert - - PowerPoint PPT Presentation

CS 61A Discussion 6 Nonlocal and Object Oriented Programming Albert Xu Slides: albertxu.xyz/teaching/cs61a/ Announcements Nonlocal changing some of your programming paradigms remember how we said that each frame has its own set of


slide-1
SLIDE 1

CS 61A Discussion 6

Nonlocal and Object Oriented Programming

Slides: albertxu.xyz/teaching/cs61a/

Albert Xu

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Nonlocal

changing some of your programming paradigms

  • remember how we said that each frame has its own set of variables?
slide-4
SLIDE 4

Nonlocal

changing some of your programming paradigms

  • remember how we said that each frame has its own set of variables?
  • if a variable doesn’t exist in the current frame(say, f2), you can look

up its value from a parent frame(say, f1)

slide-5
SLIDE 5

Nonlocal

changing some of your programming paradigms

  • remember how we said that each frame has its own set of variables?
  • if a variable doesn’t exist in the current frame(say, f2), you can look

up its value from a parent frame(say, f1)

  • …but it’s impossible for you to change the value of that parent

frame’s(f1’s) variable from inside f2!

slide-6
SLIDE 6

Nonlocal

changing some of your programming paradigms

  • remember how we said that each frame has its own set of variables?
  • if a variable doesn’t exist in the current frame(say, f2), you can look

up its value from a parent frame(say, f1)

  • …but it’s impossible for you to change the value of that parent

frame’s(f1’s) variable from inside f2!

not anymore!

slide-7
SLIDE 7

Nonlocal

changing some of your programming paradigms

  • remember how we said that each frame has its own set of variables?
  • if a variable doesn’t exist in the current frame(say, f2), you can look

up its value from a parent frame(say, f1)

  • …but it’s impossible for you to change the value of that parent

frame’s(f1’s) variable from inside f2!

not anymore! nonlocal x

slide-8
SLIDE 8

Nonlocal

changing some of your programming paradigms

  • remember how we said that each frame has its own set of variables?
  • if a variable doesn’t exist in the current frame(say, f2), you can look

up its value from a parent frame(say, f1)

  • …but it’s impossible for you to change the value of that parent

frame’s(f1’s) variable from inside f2!

not anymore! nonlocal x

this line, when run inside f2, says that every time we modify x inside the current frame(f2), instead modify f1’s x! Same thing with looking up x.

slide-9
SLIDE 9

Nonlocal Demo

demo!

slide-10
SLIDE 10

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

slide-11
SLIDE 11

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

Exhibit A

slide-12
SLIDE 12

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

Exhibit A

attempting to nonlocal a variable that already exists in the current frame

slide-13
SLIDE 13

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

Exhibit A

I L L E G A L ! ! !

attempting to nonlocal a variable that already exists in the current frame

slide-14
SLIDE 14

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

Exhibit A Exhibit B

I L L E G A L ! ! !

attempting to nonlocal a variable that already exists in the current frame

slide-15
SLIDE 15

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

Exhibit A Exhibit B

attempting to nonlocal a variable from the global frame

I L L E G A L ! ! !

attempting to nonlocal a variable that already exists in the current frame

slide-16
SLIDE 16

Pitfalls of Nonlocal

there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well

Exhibit A

attempting to nonlocal a variable that already exists in the current frame

Exhibit B

I L L E G A L ! ! ! ILLEGAL!!!

attempting to nonlocal a variable from the global frame

slide-17
SLIDE 17

UnboundLocalError

this is probably a good time to talk about it

*i borrowed this incorrect code from StackOverflow lol

slide-18
SLIDE 18

UnboundLocalError

this is probably a good time to talk about it here is code which throws an UnboundLocalError

*i borrowed this incorrect code from StackOverflow lol

slide-19
SLIDE 19

UnboundLocalError

this is probably a good time to talk about it here is code which throws an UnboundLocalError

*i borrowed this incorrect code from StackOverflow lol

…which is equivalent to

slide-20
SLIDE 20

UnboundLocalError

this is probably a good time to talk about it here is code which throws an UnboundLocalError

*i borrowed this incorrect code from StackOverflow lol

…which is equivalent to

Python has no variable declarations, so it has to guess the scope of your variables! Because you assigned a value of x inside the increment function, it assumes that x is a variable inside the increment frame. But by the order in which we evaluate the RHS of assignment before the left, we look up x before it even exists in the frame!

slide-21
SLIDE 21

UnboundLocalError

this is probably a good time to talk about it here is code which throws an UnboundLocalError

*i borrowed this incorrect code from StackOverflow lol

…which is equivalent to

Python has no variable declarations, so it has to guess the scope of your variables! Because you assigned a value of x inside the increment function, it assumes that x is a variable inside the increment frame. But by the order in which we evaluate the RHS of assignment before the left, we look up x before it even exists in the frame!

this is an analysis of the issue, but it’s not the root cause. How could anonymous SO user easily fix this?

slide-22
SLIDE 22

UnboundLocalError

this is probably a good time to talk about it here is code which throws an UnboundLocalError

*i borrowed this incorrect code from StackOverflow lol

…which is equivalent to

Python has no variable declarations, so it has to guess the scope of your variables! Because you assigned a value of x inside the increment function, it assumes that x is a variable inside the increment frame. But by the order in which we evaluate the RHS of assignment before the left, we look up x before it even exists in the frame!

this is an analysis of the issue, but it’s not the root cause. How could anonymous SO user easily fix this?

trick question!!

slide-23
SLIDE 23

UnboundLocalError

this is probably a good time to talk about it code that errors.

*i borrowed this incorrect code from StackOverflow lol

slide-24
SLIDE 24

UnboundLocalError

this is probably a good time to talk about it code that errors.

*i borrowed this incorrect code from StackOverflow lol

slide-25
SLIDE 25

UnboundLocalError

this is probably a good time to talk about it code that errors.

*i borrowed this incorrect code from StackOverflow lol

Also errors!! remember? ILLEGAL!!!

slide-26
SLIDE 26

UnboundLocalError

this is probably a good time to talk about it code that errors.

*i borrowed this incorrect code from StackOverflow lol

Also errors!! remember? ILLEGAL!!! this one works!

slide-27
SLIDE 27

UnboundLocalError

this is probably a good time to talk about it code that errors.

*i borrowed this incorrect code from StackOverflow lol

Also errors!! remember? ILLEGAL!!! this one works! unfortunately you don’t necessarily learn the global keyword in this class, just fyi!

slide-28
SLIDE 28

First, A Meme

but it’s okay! you probably won’t need to deal with this until 61B.

slide-29
SLIDE 29

Next, Data Abstraction

How would you write an ADT for a Dog? What’s the general ADT form?

slide-30
SLIDE 30

Next, Data Abstraction

How would you write an ADT for a Student? What’s the general ADT form?

def student(<attr0>, <attr1>, <attr2> ...): return [<attr0>, <attr1>, <attr2> ...]

constructor

slide-31
SLIDE 31

Next, Data Abstraction

How would you write an ADT for a Student? What’s the general ADT form?

def student(<attr0>, <attr1>, <attr2> ...): return [<attr0>, <attr1>, <attr2> ...] def attr0(student): return student[0] def attr1(student): return student[1] ...

constructor selectors

slide-32
SLIDE 32

Next, Data Abstraction

constructor selectors Let’s fill in these with some Student attributes. How would you write an ADT for a Student? What’s the general ADT form?

def student(<attr0>, <attr1>, <attr2> ...): return [<attr0>, <attr1>, <attr2> ...] def attr0(student): return student[0] def attr1(student): return student[1] ...

slide-33
SLIDE 33

Next, Data Abstraction

Here’s our completed ADT for a Student!

def student(name, ta): return [name, ta, 0] def name(student): return student[0] def ta(student): return student[1] def understanding(student): return student[2] def course(student): return “CS 61A”

constructor selectors

slide-34
SLIDE 34

Next, Data Abstraction

Some Vocab:

  • attributes: traits of a data type or object
  • instance: a specific data type made by the constructor
  • class attributes: traits that all instances share (scientific name)
  • instance attributes: traits that differ between instances (age)

def student(name, ta): return [name, ta, 0] def name(student): return student[0] def ta(student): return student[1] def understanding(student): return student[2] def course(student): return “CS 61A”

constructor selectors Here’s our completed ADT for a Student!

slide-35
SLIDE 35

OOP Formal Definitions

We’ll need to know these words to write Student

  • Class - a “template” for an object
  • represents attributes of a student, and the actions(methods) it

can take

slide-36
SLIDE 36

OOP Formal Definitions

We’ll need to know these words to write Student

  • Class - a “template” for an object
  • represents attributes of a student, and the actions(methods) it

can take

  • Instance - each object built using that template
  • a specific object, e.g. a specific student!
slide-37
SLIDE 37

OOP Formal Definitions

We’ll need to know these words to write Student

  • Class - a “template” for an object
  • represents attributes of a student, and the actions(methods) it

can take

  • Instance - each object built using that template
  • a specific object, e.g. a specific student!
  • Attributes
  • instance
  • class
slide-38
SLIDE 38

OOP Formal Definitions

We’ll need to know these words to write Student

  • Class - a “template” for an object
  • represents attributes of a student, and the actions(methods) it

can take

  • Instance - each object built using that template
  • a specific object, e.g. a specific student!
  • Attributes
  • instance
  • class
  • Method - a function bound to a particular class
  • an action a student can take, e.g. “visit_office_hours”
slide-39
SLIDE 39

Function vs. Methods

What makes this a method, and this a function?

slide-40
SLIDE 40

Function vs. Methods

What makes this a method, and this a function?

A method is a function inside a class! Think of it as an action that the object knows how to take.

slide-41
SLIDE 41

Function vs. Methods

What makes this a method, and this a function?

A method is a function inside a class! Think of it as an action that the object knows how to take. Because of this, the method drink_water needs to know what specific object it’s acting on. That’s what the self parameter is for.

slide-42
SLIDE 42

Function vs. Methods

What makes this a method, and this a function?

A method is a function inside a class! Think of it as an action that the object knows how to take. Because of this, the method drink_water needs to know what specific object it’s acting on. That’s what the self parameter is for.

albert = Dog()

slide-43
SLIDE 43

Function vs. Methods

What makes this a method, and this a function?

A method is a function inside a class! Think of it as an action that the object knows how to take. Because of this, the method drink_water needs to know what specific object it’s acting on. That’s what the self parameter is for.

Two ways to call a method:

albert = Dog()

slide-44
SLIDE 44

Function vs. Methods

What makes this a method, and this a function?

A method is a function inside a class! Think of it as an action that the object knows how to take. Because of this, the method drink_water needs to know what specific object it’s acting on. That’s what the self parameter is for.

Two ways to call a method:

albert = Dog() Dog.drink_water(albert)

slide-45
SLIDE 45

Function vs. Methods

What makes this a method, and this a function?

A method is a function inside a class! Think of it as an action that the object knows how to take. Because of this, the method drink_water needs to know what specific object it’s acting on. That’s what the self parameter is for.

Two ways to call a method:

albert = Dog() Dog.drink_water(albert) unbound method call

slide-46
SLIDE 46

Function vs. Methods

What makes this a method, and this a function?

A method is a function inside a class! Think of it as an action that the object knows how to take. Because of this, the method drink_water needs to know what specific object it’s acting on. That’s what the self parameter is for.

Two ways to call a method:

albert = Dog() Dog.drink_water(albert) albert.drink_water() unbound method call

slide-47
SLIDE 47

Function vs. Methods

What makes this a method, and this a function?

A method is a function inside a class! Think of it as an action that the object knows how to take. Because of this, the method drink_water needs to know what specific object it’s acting on. That’s what the self parameter is for.

Two ways to call a method:

albert = Dog() Dog.drink_water(albert) albert.drink_water() bound method call unbound method call

slide-48
SLIDE 48

On Inheritance

some things to remember By default, subclasses inherit their parent class’ attributes and methods

superclass subclass

Cat Pet NoisyCat

Cat inherits from Pet

slide-49
SLIDE 49

On Inheritance

some things to remember By default, subclasses inherit their parent class’ attributes and methods Think about looking up attributes and methods of an object like looking up variable in environment diagrams! Check the object itself, then its parent object, then that object’s parent, etc.

Cat Pet NoisyCat

superclass subclass Cat inherits from Pet

slide-50
SLIDE 50

On Inheritance

some things to remember By default, subclasses inherit their parent class’ attributes and methods Think about looking up attributes and methods of an object like looking up variable in environment diagrams! Check the object itself, then its parent object, then that object’s parent, etc.

Cat Pet

Cat inherits from Pet

NoisyCat

superclass subclass

slide-51
SLIDE 51

Why OOP?

  • 1. Saves time, removing repetitive code
slide-52
SLIDE 52

Why OOP?

  • 1. Saves time, removing repetitive code
  • 2. Abstract away complexity
slide-53
SLIDE 53

Why OOP?

  • 1. Saves time, removing repetitive code
  • 2. Abstract away complexity
  • 3. Inheritance!

ALL NEW! ALL NEW!

slide-54
SLIDE 54

Thanks for coming.

Have a great rest of your week! :)

Slides: albertxu.xyz/teaching/cs61a/