08 Classes Objects
08 Classes Objects
• Declaring a class:
class name:
statements
2
Fields
name = value
– Example: point.py
class Point: 1 class Point:
x = 0 2 x = 0
y = 0 3 y = 0
# main
p1 = Point()
p1.x = 2
p1.y = -5
3
Using a Class
import class
– client programs must import the classes they use
point_main.py
1 from Point import *
2
3 # main
4 p1 = Point()
5 p1.x = 7
6 p1.y = -3
7 ...
8
9 # Python objects are dynamic (can add fields any time!)
10 p1.name = "Tyler Durden"
4
Object Methods
def name(self, parameter, ..., parameter):
statements
5
"Implicit" Parameter (self)
• Java: this, implicit
public void translate(int dx, int dy) {
x += dx; // this.x += dx;
y += dy; // this.y += dy;
}
6
Exercise Answer
point.py
1 from math import *
2
3 class Point:
4 x = 0
5 y = 0
6
7 def set_location(self, x, y):
8 self.x = x
9 self.y = y
10
11 def distance_from_origin(self):
12 return sqrt(self.x * self.x + self.y * self.y)
13
14 def distance(self, other):
15 dx = self.x - other.x
16 dy = self.y - other.y
17 return sqrt(dx * dx + dy * dy)
7
Calling Methods
• A client can call the methods of an object in two ways:
– (the value of self can be an implicit or explicit parameter)
1) object.method(parameters)
or
2) Class.method(object, parameters)
• Example:
p = Point(3, -4)
p.translate(1, 5)
Point.translate(p, 1, 5)
8
Constructors
def __init__(self, parameter, ..., parameter):
statements
– Example:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
...
9
toString and __str__
def __str__(self):
return string
– equivalent to Java's toString (converts object to a string)
– invoked automatically when str or print is called
10
Complete Point Class
point.py
1 from math import *
2
3 class Point:
4 def __init__(self, x, y):
5 self.x = x
6 self.y = y
7
8 def distance_from_origin(self):
9 return sqrt(self.x * self.x + self.y * self.y)
10
11 def distance(self, other):
12 dx = self.x - other.x
13 dy = self.y - other.y
14 return sqrt(dx * dx + dy * dy)
15
16 def translate(self, dx, dy):
17 self.x += dx
18 self.y += dy
19
20 def __str__(self):
21 return "(" + str(self.x) + ", " + str(self.y) + ")"
11
Operator Overloading
• operator overloading: You can define functions so that
Python's built-in operators can be used with your class.
• See also: http://docs.python.org/ref/customization.html
12
Exercise
• Exercise: Write a Fraction class to represent rational
numbers like 1/2 and -3/8.
13
Generating Exceptions
raise ExceptionType("message")
– Example:
class BankAccount:
...
def deposit(self, amount):
if amount < 0:
raise ValueError("negative amount")
...
14
Inheritance
class name(superclass):
statements
– Example:
class Point3D(Point): # Point3D extends Point
z = 0
...
(if > 1 superclass has the same field/method, conflicts are resolved in left-to-right order)
15
Calling Superclass Methods
• methods: class.method(object, parameters)
• constructors: class.__init__(parameters)
class Point3D(Point):
z = 0
def __init__(self, x, y, z):
Point.__init__(self, x, y)
self.z = z
16