class Point: # A class that represents a 2D Point def __init__(self, x, y): ''' Initalization method, called when we create a Point. Takes 2 arguments, x and y, that must be numbers (ints or floats) ''' # Make 2 object attributes from the passed # arguments self.x = x self.y = y def __str__(self): ''' Returns a string representation of this point in the form ''' return "<"+str(self.x)+","+str(self.y)+">" def __add__(self, other_point): ''' Enables adding points via p1 + p2 Effectively the same as saying p1.add(p2) ''' return self.add(other_point) def move(self, delta_x, delta_y): ''' Moves this Point delta_x units in the x-direction and delta_y units in the y direction. delta_x and delta_y must be numbers. ''' self.x += delta_x self.y += delta_y def add(self, other_point): ''' Returns a new point that is the sum of this point and another point ''' new_x = self.x + other_point.x new_y = self.y + other_point.y new_point = Point(new_x, new_y) return new_point # creating new points p1 = Point(2, 3) p2 = Point(5, 5) # printing these points print "The value of p1 is: " + str(p1) print "The value of p2 is: " + str(p2) # using the move method p1.move(1,1) # this is the same as saying Point.move(p1, 1, 1) print "The new value of p1 is: " + str(p1) # we can add points together print "p1 + p2 = " + str(p1.add(p2)) # or p1 + p2 since we have the __add__ method