CS 61A Discussion 6Nonlocal and Object Oriented Programming
Slides: albertxu.xyz/teaching/cs61a/
Albert Xu
Announcements
Nonlocalchanging some of your programming paradigms
• remember how we said that each frame has its own set of variables?
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)
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!
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!
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
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.
Pitfalls of Nonlocalthere are two common mistakes that students make with nonlocal,
and it’s probably a good idea to learn them well
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
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
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
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
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
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
UnboundLocalErrorthis is probably a good time to talk about it
*i borrowed this incorrect code from StackOverflow lol
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
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
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!
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?
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!!
UnboundLocalErrorthis is probably a good time to talk about it
code that errors.
*i borrowed this incorrect code from StackOverflow lol
UnboundLocalErrorthis is probably a good time to talk about it
code that errors.
*i borrowed this incorrect code from StackOverflow lol
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!!!
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!
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!
First, A Meme
but it’s okay! you probably won’t need to deal with this until 61B.
Next, Data AbstractionHow would you write an ADT for a Dog?
What’s the general ADT form?
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
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
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] ...
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
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!
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
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!
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
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”
Function vs. MethodsWhat makes this a method, and this a function?
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.
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.
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()
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()
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)
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
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
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
On Inheritancesome things to remember
By default, subclasses inherit their parent class’ attributes and methods
superclass
subclass
Cat
Pet
NoisyCat
Cat inherits from Pet
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
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
Why OOP?
1. Saves time, removing repetitive code
Why OOP?
1. Saves time, removing repetitive code
2. Abstract away complexity
Why OOP?
1. Saves time, removing repetitive code
2. Abstract away complexity
3. Inheritance!ALL NEW!ALL NEW!
Thanks for coming.Have a great rest of your week! :)
Slides: albertxu.xyz/teaching/cs61a/