A class, and using objects as arguments. From http://interactivepython.org/runestone/static/pyladies/Classes/classesintro.html
import math
class Point:
""" Point class for representing and manipulating x,y coordinates. """
def __init__(self, initX, initY):
self.x = initX
self.y = initY
def getX(self):
return self.x
def getY(self):
return self.y
def distanceFromOrigin(self):
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
def __str__(self):
return "x=" + str(self.x) + ", y=" + str(self.y)
def distance(point1, point2):
xdiff = point2.getX()-point1.getX()
ydiff = point2.getY()-point1.getY()
dist = math.sqrt(xdiff**2 + ydiff**2)
return dist
p = Point(4,3)
q = Point(0,0)
print(distance(p,q))
print(p)
print "It is " + str(p)