Lec 6
Lec 6
PROGRAMMING 1
CS5800: Lecture 6
1
LEARNING OBJECTIVES
OOP concepts
Creating objects
Class attributes, methods
Querying types
Special methods
2
RECAP: FUNCTIONS
3
OBJECTS AND DATA TYPES
4
OBJECT-ORIENTED
PROGRAMMING (OOP)
5
PYTHON OBJECT TYPES
6
EXAMPLE: [1, 2, 3, 4]
L = [1, 2, 3, 4] is of type list
Internal representation: linked list of cells
1 2 3 4
Follow pointer to
Interface to manipulate lists: the next index
◦ L[i], L[i:j], +
◦ len(), min(), max(), del(L[i])
◦ L.append() ,L.extend(), L.count(), L.index(),
L.insert(), L.pop(), L.remove(), L.reverse(), L.sort()
9
DEFINE YOUR OWN OBJECT TYPE
10
WHAT ARE CLASS ATTRIBUTES?
11
Go to Moodle quiz ‘Internal attributes’
DEFINING HOW TO CREATE AN
INSTANCE OF A CLASS
print(c.x, origin.x)
use "dot" to access the
object attribute
14
WHAT IS A METHOD?
15
Go to Moodle quiz ‘Create class’
DEFINE A METHOD FOR THE
Coordinate CLASS
class Coordinate(object): Refers to any
def __init__(self, x, y): instance
self.x = x Another parameter
of to method
self.y = y
def distance(self, other): Dot notation to
x_diff_sq = (self.x - other.x)**2 access data
y_diff_sq = (self.y - other.y)**2 attributes
return (x_diff_sq + y_diff_sq)**0.5
17
TWO WAYS OF USING A METHOD
c = Coordinate(3,4) c = Coordinate(3,4)
origin = Coordinate(0,0) origin = Coordinate(0,0)
print(c.distance(origin)) print(Coordinate.distance(c, origin))
18
Go to Moodle quiz ‘Create method’
PRINT REPRESENTATION OF AN
OBJECT
>>> c = Coordinate(3,4)
>>> print(c)
<__main__.Coordinate object at 0x10aa29780>
20
DEFINING YOUR OWN STRING
REPRESENTATION
class Coordinate(object): >>> c = Coordinate(3, 4)
def __init__(self, x, y): >>> print(c)
self.x = x <3,4>
self.y = y
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
def __str__(self):
return "<" + str(self.x) + "," + str(self.y) + ">"
22
SPECIAL METHODS
SPECIAL OPERATORS
+, -, ==, <, >, len(), str(), etc.
+, -, ==, <, >, len(), print, and many
◦ https://docs.python.org/3/reference/
others
datamodel.html#basic-customization
https://docs.python.org/3/reference/datamodel.html#basic-customization
Canprint,
like overridecan
these to work
override with
these toyour
workclass
with your class
Definethem
define themwith
withdouble
double underscores
underscoresbefore/after
before/after
__add__(self, other) self + other
__sub__(self, other) self - other
__eq__(self, other) self == other
__lt__(self, other) self < other
__len__(self) len(self)
__str__(self) print self
... andhttps://docs.python.org/3/library/operator.html
See others
23
Go to Moodle quiz ‘Special methods’
EXAMPLE: FRACTIONS
25
IMPLEMENTING USING
THE CLASS vs THE CLASS
write code from two different perspectives
implementing a new using the new object type in
object type with a class code
• define the class • create instances of the
• define data attributes object type
(WHAT IS the object) • do operations with them
• define methods
(HOW TO use the object)
6.0001 26
LECTURE 9 3
CLASS DEFINITION INSTANCE
OF AN OBJECT TYPE vs OF A CLASS
class name is the type instance is one specific object
class Coordinate(object) coord = Coordinate(1,2)