Skip to content

Instantly share code, notes, and snippets.

@eudaimonious
Created January 31, 2014 22:14
Show Gist options
  • Save eudaimonious/8744358 to your computer and use it in GitHub Desktop.
Save eudaimonious/8744358 to your computer and use it in GitHub Desktop.
A class method that takes another object as an argument and returns a new object. From http://interactivepython.org/runestone/static/pyladies/Classes/classesintro.html
class Point:
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 halfway(self, target):
mx = (self.x + target.x)/2
my = (self.y + target.y)/2
return Point(mx, my)
p = Point(3,4)
q = Point(5,12)
mid = p.halfway(q)
print(mid)
print(mid.getX())
print(mid.getY())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment