# points.py import math # Classes and objects # For the Point class an example definition would look like this: class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def __str__(self): return '(' + str(self.x) + ', ' + str(self.y) + ')' def distance_from_origin(self): return math.sqrt(self.x * self.x + self.y * self.y) def distance(self, other): dx = math.fabs(self.x - other.x) dy = math.fabs(self.y - other.y) return math.sqrt(dx * dx + dy * dy) # Now that a class (Point) is defined, let's create some # instances (objects) p1 = Point(3, 4) p2 = Point(10, 20) print p1 print str(p1) # uses __str__ if defined # without __str__ defined, str(p1) would print something like # . That is, Python language tries to # print as much as it knows about the object as a printed representation. # We as a programmer would not be very satisfied with that printed # representation. We can provide more information by defining a function that # can produce more useful printed representation. '__str__' is that function. # So, with __str__ defined as above str(p1) would produce '(3, 4)'. # This computes the distance from the origin of p1. print p1.distance_from_origin() # This one between p1 and p2. print p1.distance(p2) # We saw how a class defined first with details initially and create instances # based on that class definition above with the class Point. # # Well, Python allows another way to create objects. We initially create a # class name, e.g., Rectangle, without the details as below: class Rectangle: pass # Then, create an instance, e.g., box below: box = Rectangle() # Then, start adding details to the object incrementally as we go. # E.g., we can add fields such as width, height, etc. as below: box.width = 100.0 box.height = 200.0 box.corner = Point() box.corner.x = 0.0 box.corner.y = 0.0 # We can define a function that takes a rectangle like this: def find_center(box): p = Point() p.x = box.corner.x + box.width/2.0 p.y = box.corner.y - box.height/2.0 return p center = find_center(box) print str(center) # Set of objects # # Once we know how to create objects, we would probably want to deal with # a collection of ojects as we program to do interesting things. For example, # we may want to create a list of student objects and deal with the list # to find the students who are majoring in Biology, say. You can use your # imagination as to how this sort of data structures would be useful. Anyway, # let us see how to create a list of objects. Well, let's do it using a # function. # Creates a list of point objects, m * n of them: # def create_points_set(m, n): points = [] i = 0 j = 0 while i < m: while j < n: points.append(Point(i, j)) j = j + 1 i = i + 1 return points # Use the function to create a list: ps = create_points_set(3, 5) # Once you create a list, you can do whatever you want to do with it. Here, # print each by using a function: def print_points_set(ps): for p in ps: print p print_points_set(ps) # As I said earlier, I am not going to get into objects in detail in this # tutorial, but it gives you an idea on how objects are created and used. # This level of coverage would be useful to understand that stuff we deal # with down stream. # For more information on classes and objects in Python, see Chapter 13 # of [DEM].