Reputation: 323
I have created a parent Class (Geometry) and Sub-Class (Point) with several methods. I am trying to use the methods in the two classes to run processes in the test function. I am having a hard time access those methods within a function. I have created three points in the test function, P1 = (0,3), P2 = (-3,7), and P3 = (-3,7), and want to perform task on them. For instance, def init(self, x, y): is suppose to return the x,y coordinate of a point listed in the test function. I tried all of the following: print(test(Point(p1))), print test(p1.Point), and print p1.Point. None of them worked. Could someone please provide guidance on how I can execute that?
My code:
class Geometry(object):
uniqueID = -1
def __init__(self):
Geometry.uniqueID += 1
self.id = Geometry.uniqueID
class Point(Geometry):
def __init__(self, x, y):
Geometry.__init__(self)
self.x = x
self.y = y
def __str__(self):
return '(%.2f, %.2f)' % (int(self.x*100)/100.0, int(self.y*100)/100.0)
def __eq__(self, point):
if type(point) is Point:
if self.x == point.x and self.y == point.y:
return True
else:
return False
else:
raise Exception("The input parameter has to be a Point object!")
def identify(self, other):
if type(other) is Point:
if self.id == other.id:
return True
else:
return False
else:
raise Exception("The input parameter has to be a Point object!")
def distance(self, other):
if type(other) is Point:
x_square = (other.x - self.x) ** 2
y_square = (other.y - self.y) ** 2
d = (x_square + y_square) ** 0.5
return float(d)
else:
raise Exception("The input parameter has to be a Point object!")
def quadrant(self):
output = ""
if self.x > 0 and self.y > 0:
output = "Quad I"
elif self.x < 0 and self.y > 0:
output = "Quad II"
elif self.x < 0 and self.y < 0:
output = "Quad III"
elif self.x > 0 and self.y < 0:
output = "Quad IV"
elif self.x == 0:
output = "Y-axis"
elif self.y == 0:
output = "X-axis"
elif self.x == 0 and self.y == 0:
output = "Origin"
return output
def test(p1,p2,p3):
p1 = Point(0, 3)
p2 = Point(-3, 7)
p3 = Point(-3, 7)
def collinear(p1, p2, p3):
p1x = p1.x
p1y = p1.y
# m1 = ?
# m2 = ?
def main():
print (p1.Point)
if __name__ == "__main__":
main()
Upvotes: 0
Views: 383
Reputation: 257
With the __ str __ implementation you have , I'm guessing you want to explicitly display the co-ordinates when printing an object. In that case, I was able to import your code and find out that your print logic works well.
Below is the working code:
class Geometry(object):
uniqueID = -1
def __init__(self):
Geometry.uniqueID += 1
self.id = Geometry.uniqueID
class Point(Geometry):
def __init__(self, x, y):
Geometry.__init__(self)
self.x = x
self.y = y
def __str__(self):
return '(%.2f, %.2f)' % (int(self.x*100)/100.0, int(self.y*100)/100.0)
def __eq__(self, point):
if type(point) is Point:
if self.x == point.x and self.y == point.y:
return True
else:
return False
else:
raise Exception("The input parameter has to be a Point object!")
def identify(self, other):
if type(other) is Point:
if self.id == other.id:
return True
else:
return False
else:
raise Exception("The input parameter has to be a Point object!")
def distance(self, other):
if type(other) is Point:
x_square = (other.x - self.x) ** 2
y_square = (other.y - self.y) ** 2
d = (x_square + y_square) ** 0.5
return float(d)
else:
raise Exception("The input parameter has to be a Point object!")
def quadrant(self):
output = ""
if self.x > 0 and self.y > 0:
output = "Quad I"
elif self.x < 0 and self.y > 0:
output = "Quad II"
elif self.x < 0 and self.y < 0:
output = "Quad III"
elif self.x > 0 and self.y < 0:
output = "Quad IV"
elif self.x == 0:
output = "Y-axis"
elif self.y == 0:
output = "X-axis"
elif self.x == 0 and self.y == 0:
output = "Origin"
return output
def test():
p1 = Point(0, 3)
p2 = Point(-3, 7)
p3 = Point(-3, 7)
return p1, p2, p3
def collinear(p1, p2, p3):
p1x = p1.x
p1y = p1.y
# m1 = ?
# m2 = ?
if __name__ == "__main__":
p1, p2, p3= test()
#Either this
print "p1 is " + p1.__str__()
print "p2 is " + p2.__str__()
print "p3 is " + p3.__str__()
print "\n*******************\n"
#Or this
print p1
print p2
print p3
Can you verify. Also can you make sure if the Point(0.3, 0.4) is local to the function where you are actually doing a print.
Sample output for the above:
p1 is (0.00, 3.00)
p2 is (-3.00, 7.00)
p3 is (-3.00, 7.00)
*******************
(0.00, 3.00)
(-3.00, 7.00)
(-3.00, 7.00)
Upvotes: 1