You are on page 1of 16

Classes and objects

Madhavan Mukund
https://www.cmi.ac.in/~madhavan

Programming, Data Structures and Algorithms using Python


Week 1
Classes and objects
Abstract datatype
Stores some information
Designated functions to manipulate the information
For instance, stack: last-in, first-out, push(), pop()

Madhavan Mukund Classes and objects PDSA using Python Week 1 2/5
Classes and objects
Abstract datatype
Stores some information
Designated functions to manipulate the information
For instance, stack: last-in, first-out, push(), pop()

Separate the (private) implementation from the (public) specification

Madhavan Mukund Classes and objects PDSA using Python Week 1 2/5
Classes and objects
Abstract datatype
Stores some information
Designated functions to manipulate the information
For instance, stack: last-in, first-out, push(), pop()

Separate the (private) implementation from the (public) specification


Class
Template for a data type
How data is stored
How public functions manipulate data

Madhavan Mukund Classes and objects PDSA using Python Week 1 2/5
Classes and objects
Abstract datatype
Stores some information
Designated functions to manipulate the information
For instance, stack: last-in, first-out, push(), pop()

Separate the (private) implementation from the (public) specification


Class
Template for a data type
How data is stored
How public functions manipulate data

Object
Concrete instance of template

Madhavan Mukund Classes and objects PDSA using Python Week 1 2/5
Example: 2D points
A point has coordinates (x, y ) class Point:
init () initializes internal values def __init__(self,a=0,b=0):
x, y self.x = a
First parameter is always self self.y = b
Here, by default a point is at (0, 0)

Madhavan Mukund Classes and objects PDSA using Python Week 1 3/5
Example: 2D points
A point has coordinates (x, y ) class Point:
init () initializes internal values def __init__(self,a=0,b=0):
x, y self.x = a
First parameter is always self self.y = b
Here, by default a point is at (0, 0)
def translate(self,deltax,deltay):
Translation: shift a point by (∆x, ∆y ) self.x += deltax
(x, y ) 7→ (x + ∆x, y + ∆y ) self.y += deltay

Madhavan Mukund Classes and objects PDSA using Python Week 1 3/5
Example: 2D points
A point has coordinates (x, y ) class Point:
init () initializes internal values def __init__(self,a=0,b=0):
x, y self.x = a
First parameter is always self self.y = b
Here, by default a point is at (0, 0)
def translate(self,deltax,deltay):
Translation: shift a point by (∆x, ∆y ) self.x += deltax
(x, y ) 7→ (x + ∆x, y + ∆y ) self.y += deltay
Distance from the origin
p def odistance(self):
d= x2 + y2
import math
d = math.sqrt(self.x*self.x +
self.y*self.y)
return(d)
Madhavan Mukund Classes and objects PDSA using Python Week 1 3/5
Polar coordinates
(r , θ) instead of (x, y ) import math
class Point:
p
r= x2 + y2
−1 def __init__(self,a=0,b=0):
θ = tan (y /x)
self.r = math.sqrt(a*a + b*b)
if a == 0:
self.theta = math.pi/2
else:
self.theta = math.atan(b/a)

Madhavan Mukund Classes and objects PDSA using Python Week 1 4/5
Polar coordinates
(r , θ) instead of (x, y ) import math
class Point:
p
r= x2 + y2
−1 def __init__(self,a=0,b=0):
θ = tan (y /x)
self.r = math.sqrt(a*a + b*b)
Distance from origin is just r if a == 0:
self.theta = math.pi/2
else:
self.theta = math.atan(b/a)

def odistance(self):
return(self.r)

Madhavan Mukund Classes and objects PDSA using Python Week 1 4/5
Polar coordinates
(r , θ) instead of (x, y ) def translate(self,deltax,deltay):
p
r= x2 + y2 x = self.r*math.cos(self.theta)
θ = tan −1
(y /x)
y = self.r*math.sin(self.theta)
x += deltax
Distance from origin is just r y += deltay
self.r = math.sqrt(x*x + y*y)
Translation
if x == 0:
Convert (r , θ) to (x, y ) self.theta = math.pi/2
x = r cos θ, y = r sin θ else:
Recompute r , θ from self.theta = math.atan(y/x)
(x + ∆x, y + ∆y )

Madhavan Mukund Classes and objects PDSA using Python Week 1 4/5
Polar coordinates
(r , θ) instead of (x, y ) def translate(self,deltax,deltay):
p
r= x2 + y2 x = self.r*math.cos(self.theta)
θ = tan −1
(y /x)
y = self.r*math.sin(self.theta)
x += deltax
Distance from origin is just r y += deltay
self.r = math.sqrt(x*x + y*y)
Translation
if x == 0:
Convert (r , θ) to (x, y ) self.theta = math.pi/2
x = r cos θ, y = r sin θ else:
Recompute r , θ from self.theta = math.atan(y/x)
(x + ∆x, y + ∆y )

Interface has not changed


User need not be aware whether
representation is (x, y ) or (r , θ)
Madhavan Mukund Classes and objects PDSA using Python Week 1 4/5
Special functions
init () — constructor

Madhavan Mukund Classes and objects PDSA using Python Week 1 5/5
Special functions
init () — constructor class Point:

str () — convert object to string ...


str(o) == o. str()
Implicitly invoked by print() def __str__(self):
return(
’(’+str(self.x)+’,’
+str(self.y)+’)’
)

Madhavan Mukund Classes and objects PDSA using Python Week 1 5/5
Special functions
init () — constructor class Point:

str () — convert object to string ...


str(o) == o. str()
Implicitly invoked by print() def __str__(self):
return(
add () ’(’+str(self.x)+’,’
Implicitly invoked by + +str(self.y)+’)’
)

def __add__(self,p):
return(Point(self.x + p.x,
self.y + p.y))

Madhavan Mukund Classes and objects PDSA using Python Week 1 5/5
Special functions
init () — constructor class Point:

str () — convert object to string ...


str(o) == o. str()
Implicitly invoked by print() def __str__(self):
return(
add () ’(’+str(self.x)+’,’
Implicitly invoked by + +str(self.y)+’)’
)
Similarly
mult () invoked by *
def __add__(self,p):
lt () invoked by < return(Point(self.x + p.x,
ge () invoked by >= self.y + p.y))
...

Madhavan Mukund Classes and objects PDSA using Python Week 1 5/5

You might also like