cs 61a discussion 6 - albertxu.xyzalbertxu.xyz/teaching/cs61a/slides/disc06.pdf · changing some of...
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/1.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/2.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/3.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/4.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/5.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/6.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/7.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/8.jpg)
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 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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/10.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/11.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/12.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/13.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/14.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/15.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/16.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/17.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/18.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/19.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/20.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/21.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/22.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/23.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/24.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/25.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/26.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/27.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/28.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/29.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/30.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/31.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/32.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/33.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/34.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/35.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/36.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/37.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/38.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/39.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/40.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/41.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/42.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/43.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/44.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/45.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/46.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/47.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/48.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/49.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/50.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/51.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/52.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/53.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022051815/603e584b7ebbcf6fae29a9f0/html5/thumbnails/54.jpg)
Thanks for coming.Have a great rest of your week! :)
Slides: albertxu.xyz/teaching/cs61a/