Weeks 13: More Classes and Objects
Class Recordings
To access recordings, sign in with your @swarthmore.edu account.
Monday |
||
Wednesday |
Section 1 (Joshua) |
|
Friday |
Section 1 (Joshua) |
Announcements
-
Quiz 5 is this Friday, 12/03.
Monday
Objects, scope, and internal method calls
Inside a class definition, the object is referred to as self
. If we want to
access its data, we use dot notation e.g., self.cooked
is that records
whether or not a pizza is cooked. The cooked
variable’s scope is the object
itself rather than the function it was originally defined in.
Similarly, from within a method of a object, you can call another method by
referring to the self
variable. For example, if a class has two methods,
method1(self, …)
and method2(self, …)
, you can call method1
from
within method2
:
class Thing(object):
def method1(self, x, y, z):
...
def method2(self, ...):
...
self.method1(a, b, c) # Call method1() on self from within method2
...
Why objects?
Objects provide encapsulation. In computer science, encapsulation can mean one of two related things:
-
A mechanism for restricting access to some of the object’s components.
-
A mechanism for bundling of data with methods operating on that data.
Classes and objects provide both mechanisms. On larger programming projects, it is common to have different people work on different parts of the program. Classes are a good place to divide the work. In this case, the class writer and the class user can agree on an interface. The interface specifies what methods the class has and what they should do. The class user doesn’t need to know or care how a class is implemented, only how to use the objects. The class writer doesn’t need to know or care how a class is used, only how to implement the interface.
Object-oriented programming also facilitates modularity. Modular programming is a software design technique that focuses on separating the functionality of a program into independent, interchangable pieces (aka "modules"), so that each piece contains everything needed to perform one aspect of the desired functionality.
Class definitions provide reusability, i.e., they let you create/reuse functionality while hiding technical details. With classes, you can rapidly create new and complex code by grabbing existing code "off the shelf" and reusing it for novel purposes.
Exercises: Implement Pizza class and test
-
Take some time to implement and test the
addTopping()
method. For now, only allow one topping per pizza. Also, make sure the pizza hasn’t yet been cooked. -
Analyze and test the
serveSlice()
function. What needs to happen before a slice can be served? -
Implement and test a
removeTopping()
function, which removes the topping if the pizza hasn’t yet been made. -
If you have time, modify the Pizza class to allow for multiple toppings. How does this change the data in the Pizza class? How would this change the methods?
Wednesday
Exercise: Aquarium
In fish.py, we’ve partially implemented a Fish class representing a fish, using the Zelle graphics library. Complete the Fish class by adding, at a minimum, an eye and a pupil to the Fish class. Test the Fish
class by creating ten fish with random locations, sizes, and colors. Then, write a moveFish(dx)
method, which moves the fish to the right by dx
pixels.
Friday
Quiz 5
-
Quiz 5 is today Friday, 12/03.
Continue Wednesday’s Aquarium Exercise
Add a method offScreen(self)
to your Fish class to test whether the fish is completely out of the window to the right, and if so it moves it back to just off the left side of the window.
do not worry about detecting the exact instant the fish is off the screen; a rough overestimate will suffice. |
Use this method to make the ten fish swim around and around the aquarium.