CS 61A Discussion 6
Nonlocal and Object Oriented Programming
Slides: albertxu.xyz/teaching/cs61a/
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
Slides: albertxu.xyz/teaching/cs61a/
changing some of your programming paradigms
changing some of your programming paradigms
up its value from a parent frame(say, f1)
changing some of your programming paradigms
up its value from a parent frame(say, f1)
frame’s(f1’s) variable from inside f2!
changing some of your programming paradigms
up its value from a parent frame(say, f1)
frame’s(f1’s) variable from inside f2!
not anymore!
changing some of your programming paradigms
up its value from a parent frame(say, f1)
frame’s(f1’s) variable from inside f2!
not anymore! nonlocal x
changing some of your programming paradigms
up its value from a parent frame(say, f1)
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.
demo!
there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well
there are two common mistakes that students make with nonlocal, and it’s probably a good idea to learn them well
Exhibit A
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
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
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
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
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
this is probably a good time to talk about it
*i borrowed this incorrect code from StackOverflow lol
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
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
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 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?
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!!
this is probably a good time to talk about it code that errors.
*i borrowed this incorrect code from StackOverflow lol
this is probably a good time to talk about it code that errors.
*i borrowed this incorrect code from StackOverflow lol
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 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!
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!
but it’s okay! you probably won’t need to deal with this until 61B.
How would you write an ADT for a Dog? What’s the general ADT form?
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
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
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] ...
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
Some Vocab:
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!
We’ll need to know these words to write Student
can take
We’ll need to know these words to write Student
can take
We’ll need to know these words to write Student
can take
We’ll need to know these words to write Student
can take
What makes this a method, and this a function?
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.
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.
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()
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()
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)
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
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
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
some things to remember By default, subclasses inherit their parent class’ attributes and methods
superclass subclass
Cat Pet NoisyCat
Cat inherits from Pet
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
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
ALL NEW! ALL NEW!
Slides: albertxu.xyz/teaching/cs61a/