Week 12: Object-Oriented Programming
Lectures:
You must be logged on to your swarthmore.edu account for the youtube lecture videos to be viewable |
Introduction
We’ve spent a lot of time talking about top-down design as a way to help you modularize your programs. The strength of this approach is that it allows you to make abstractions and hide details in lower-level functions.
Another approach to creating modular programs is object-oriented design.
Remember that objects know stuff and can do stuff.
Defining a new class of objects can simplify the structure of a program by allowing a single variable to store a set of related data. These objects may also have complex behavior that is captured in the methods of the class.
This idea is called ENCAPSULATION. We can take a large collection of data and methods and enscapsulate them into a single object.
Examples from the graphics library
The graphics library that we used earlier in the semester is designed in an object-oriented style. An object consists of a set of related data and a set of operations to manipulate that data. Data is stored in instance variables and is manipulated using methods. Every object is an instance of some class and it is the class definition that determines the attributes of the object.
For example, recall the Circle
class. To create a circle object we
need to know its center point and its radius.
c1 = Circle(Point(100, 100), 50)
The c1
object is an instance of the Circle
class. We can use various
methods on this object.
c1.getRadius() will return 50
c1.getCenter() will return the point at 100, 100
Previously we learned how to use existing classes. This week we will learn how to define our own classes.
Defining our own classes
A CLASS defines a new type and is a collection of data and methods.
Methods are just functions. Placing a function inside a class makes it a method of that class, rather than just a stand-alone function. Here is the syntax for defining a class:
class <class-name>(object):
<method definitions>
Example: Point
class
Certain methods in a class have special meaning to Python. These methods have names that begin with two underscore characters.
The special method init
is the object constructor. Note that all
special method names in Python begin and end with two underscore
characters. Python uses this method to initialize the instance
variables of a new object.
For a Point
we need to know its x
and y
coordinates, but the
first parameter of the init
method is named self
. After that,
we put x
and y
. This first parameter is special and appears as the
first parameter of every method.
The self
parameter holds a pointer to the object on which the method
is acting.
Another special method in Python is str
(again with two underscores
before and after the name). This method will automatically be called
whenever you print
an instance of the object.
Most class definitions will also include methods which are known as getters and setters. Getter methods allow you to see the current value of a particular class variable. Setter methods allow you to change the current value of a particular class variable.
class Point(object):
def __init__(self, x, y):
"""Constructor is automatically called when you create a new Point"""
self.x = x
self.y = y
def __str__(self):
"""Returns a string of data in object and is called when you print"""
return "x: %d y: %d" % (self.x, self.y)
def setX(self, newx):
"""Setter method"""
self.x = newx
def setY(self, newy):
"""Setter method"""
self.y = newy
def getX(self):
"""Getter method"""
return self.x
def getY(self):
"""Getter method"""
return self.y
def main():
"""Test out the Point class"""
p1 = Point(1, 2)
p2 = Point(5, 3)
print(p1)
print(p2)
p1.setX(10)
print(p1.getX())
main()
To call the init
method of a class you need to use the
class name, as was done above when creating p1
and p2
in main
.
To call the str
method of a class you need to print
the object.
What does self
represent?
In Python, when defining the methods for a class, the first parameter
should always be self
. This variable points to the location of the
entire object in memory.
Using self
, we can access all of the data (what the object knows),
without having to pass it around between the methods. We simply do
self.<variable>
to access a piece of data stored in the object.
This ability to directly access the data within the object makes the methods simpler and easier to implement.