cs 61a discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of...

54
CS 61A Discussion 6 Nonlocal and Object Oriented Programming Slides: albertxu.xyz/teaching/cs61a/ Albert Xu

Upload: others

Post on 10-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

CS 61A Discussion 6Nonlocal and Object Oriented Programming

Slides: albertxu.xyz/teaching/cs61a/

Albert Xu

Page 2: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Announcements

Page 3: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Nonlocalchanging some of your programming paradigms

• remember how we said that each frame has its own set of variables?

Page 4: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Nonlocalchanging 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)

Page 5: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Nonlocalchanging 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!

Page 6: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Nonlocalchanging 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!

Page 7: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Nonlocalchanging 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

Page 8: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Nonlocalchanging 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 xthis 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.

Page 9: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Nonlocal Demo

demo!

Page 10: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Pitfalls of Nonlocalthere are two common mistakes that students make with nonlocal,

and it’s probably a good idea to learn them well

Page 11: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Pitfalls of Nonlocalthere are two common mistakes that students make with nonlocal,

and it’s probably a good idea to learn them well

Exhibit A

Page 12: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Pitfalls of Nonlocalthere are two common mistakes that students make with nonlocal,

and it’s probably a good idea to learn them well

Exhibit Aattempting to

nonlocal a variable that already exists in

the current frame

Page 13: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Pitfalls of Nonlocalthere are two common mistakes that students make with nonlocal,

and it’s probably a good idea to learn them well

Exhibit A

ILLEGAL!!!attempting to nonlocal a variable

that already exists in the current frame

Page 14: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Pitfalls of Nonlocalthere are two common mistakes that students make with nonlocal,

and it’s probably a good idea to learn them well

Exhibit A

Exhibit B

ILLEGAL!!!attempting to nonlocal a variable

that already exists in the current frame

Page 15: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Pitfalls of Nonlocalthere are two common mistakes that students make with nonlocal,

and it’s probably a good idea to learn them well

Exhibit A

Exhibit Battempting to

nonlocal a variable from the global frame

ILLEGAL!!!attempting to nonlocal a variable

that already exists in the current frame

Page 16: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Pitfalls of Nonlocalthere are two common mistakes that students make with nonlocal,

and it’s probably a good idea to learn them well

Exhibit Aattempting to

nonlocal a variable that already exists in

the current frame

Exhibit B

ILLEGAL!!!

ILLEGAL!!!

attempting to nonlocal a variable

from the global frame

Page 17: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

UnboundLocalErrorthis is probably a good time to talk about it

*i borrowed this incorrect code from StackOverflow lol

Page 18: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

UnboundLocalErrorthis is probably a good time to talk about it

here is code which throws an UnboundLocalError

*i borrowed this incorrect code from StackOverflow lol

Page 19: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

UnboundLocalErrorthis 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

Page 20: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

UnboundLocalErrorthis 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!

Page 21: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

UnboundLocalErrorthis 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?

Page 22: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

UnboundLocalErrorthis 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!!

Page 23: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

UnboundLocalErrorthis is probably a good time to talk about it

code that errors.

*i borrowed this incorrect code from StackOverflow lol

Page 24: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

UnboundLocalErrorthis is probably a good time to talk about it

code that errors.

*i borrowed this incorrect code from StackOverflow lol

Page 25: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

UnboundLocalErrorthis is probably a good time to talk about it

code that errors.

*i borrowed this incorrect code from StackOverflow lol

Also errors!! remember? ILLEGAL!!!

Page 26: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

UnboundLocalErrorthis 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!

Page 27: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

UnboundLocalErrorthis 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!

Page 28: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

First, A Meme

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

Page 29: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Next, Data AbstractionHow would you write an ADT for a Dog?

What’s the general ADT form?

Page 30: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Next, Data AbstractionHow would you write an ADT for a Student?

What’s the general ADT form?

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

Page 31: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Next, Data AbstractionHow 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

Page 32: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

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] ...

Page 33: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Next, Data AbstractionHere’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

Page 34: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

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!

Page 35: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

OOP Formal DefinitionsWe’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

Page 36: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

OOP Formal DefinitionsWe’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!

Page 37: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

OOP Formal DefinitionsWe’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

Page 38: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

OOP Formal DefinitionsWe’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”

Page 39: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Function vs. MethodsWhat makes this a method, and this a function?

Page 40: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Function vs. MethodsWhat 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.

Page 41: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Function vs. MethodsWhat 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.

Page 42: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Function vs. MethodsWhat 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()

Page 43: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Function vs. MethodsWhat 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()

Page 44: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Function vs. MethodsWhat 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)

Page 45: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Function vs. MethodsWhat 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

Page 46: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Function vs. MethodsWhat 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

Page 47: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Function vs. MethodsWhat 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

Page 48: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

On Inheritancesome things to remember

By default, subclasses inherit their parent class’ attributes and methods

superclass

subclass

Cat

Pet

NoisyCat

Cat inherits from Pet

Page 49: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

On Inheritancesome 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

Page 50: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

On Inheritancesome 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

Page 51: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Why OOP?

1. Saves time, removing repetitive code

Page 52: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Why OOP?

1. Saves time, removing repetitive code

2. Abstract away complexity

Page 53: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Why OOP?

1. Saves time, removing repetitive code

2. Abstract away complexity

3. Inheritance!ALL NEW!ALL NEW!

Page 54: CS 61A Discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of your programming paradigms • remember how we said that each frame has its own

Thanks for coming.Have a great rest of your week! :)

Slides: albertxu.xyz/teaching/cs61a/