Lecture 17 - Python Classes
Lecture 17 - Python Classes
1
OBJECTS
6.100L Lecture 17
OBJECT ORIENTED
PROGRAMMING (OOP)
6.100L Lecture 17
WHAT ARE OBJECTS?
6.100L Lecture 17
EXAMPLE:
[1,2,3,4] has type list
6.100L Lecture 17
REAL-LIFE EXAMPLES
6.100L Lecture 17
ADVANTAGES OF OOP
6.100L Lecture 17
BIG IDEA
You write the class so you
make the design decisions.
You decide what data represents the class.
You decide what operations a user can do with the class.
6.100L Lecture 17
Implementing the class Using the class
6.100L Lecture 17
A PARALLEL with FUNCTIONS
10
6.100L Lecture 17
COORDINATE TYPE
DESIGN DECISIONS
Can create instances of a Decide what data elements
Coordinate object constitute an object
• In a 2D plane
• A coordinate is defined by
(3 , 4) an x and y value
11
6.100L Lecture 17
Implementing the class Using the class
class Coordinate(object):
#define attributes here
6.100L Lecture 17
WHAT ARE ATTRIBUTES?
13
6.100L Lecture 17
Implementing the class Using the class
6.100L Lecture 17
Image © source unknown. All rights
reserved. This content is excluded from
our Creative Commons license. For more
WHAT is self?
information, see https://ocw.mit.edu/
help/faq-fair-use/
ROOM EXAMPLE
Think of the class definition as a Now when you create ONE instance
blueprint with placeholders for (name it living_room), self becomes
actual items this actual object
self has a chair living_room has a blue chair
self has a coffee table living_room has a black table
self has a sofa living_room has a white sofa
Can make many instances using
the same blueprint
15
6.100L Lecture 17
BIG IDEA
When defining a class,
we don’t have an actual
tangible object here.
It’s only a definition.
16
6.100L Lecture 17
Implementing the class Using the class
6.100L Lecture 17
VISUALIZING INSTANCES
18
6.100L Lecture 17
VISUALIZING INSTANCES:
in memory
Make another instance using
a variable
a = 0 Type: Coordinate
c x: 3
orig = Coordinate(a,a) y: 4
orig.x a 0
19
6.100L Lecture 17
VISUALIZING INSTANCES:
draw it
class Coordinate(object):
def __init__(self, xval, yval):
self.x = xval
self.y = yval
(3 , 4)
c
c = Coordinate(3,4)
origin = Coordinate(0,0)
print(c.x)
print(origin.x)
(0 , 0)
origin
20
6.100L Lecture 17
WHAT IS A METHOD?
Procedural attribute
Think of it like a function that works only with this class
Python always passes the object as the first argument
Convention is to use self as the name of the first argument of all
methods
21
6.100L Lecture 17
Implementing the class Using the class
DEFINE A METHOD
FOR THE Coordinate CLASS
class Coordinate(object):
def __init__(self, xval, yval):
self.x = xval
self.y = yval
def distance(self, other):
x_diff_sq = (self.x-other.x)**2
y_diff_sq = (self.y-other.y)**2
return (x_diff_sq + y_diff_sq)**0.5
Other than self and dot notation, methods behave just
like functions (take params, do operations, return)
22
6.100L Lecture 17
HOW TO CALL A METHOD?
Familiar?
my_list.append(4)
my_list.sort()
23
6.100L Lecture 17
Implementing the class Using the class
6.100L Lecture 17
VISUALIZING INVOCATION
25
6.100L Lecture 17
VISUALIZING INVOCATION
6.100L Lecture 17
Implementing the class Using the class
27
6.100L Lecture 17
BIG IDEA
The . operator accesses
either data attributes or
methods.
Data attributes are defined with self.something
Methods are functions defined inside the class with self as the first parameter.
28
6.100L Lecture 17
THE POWER OF OOP
29
6.100L Lecture 17
MITOpenCourseWare
https://ocw.mit.edu
For information about citing these materials or our Terms ofUse,visit: https://ocw.mit.edu/terms.
30