I'm pretty sure this has been asked many times but I'm still not sure how to implement the multiple constructors in Python. I know that in python, I can only have one constructor unlike in java or C# or C++. I'm still pretty new to it. Long story short, I need to implement a line object. The line will be represented by the function y = ax + b. So the only things I need to store in the line are a, b and a boolean for the special case where the line is vertical (a = infinity). In that case, a will store the x position of the line. To create a line, I have 3 ways. 1 is to directly put in a, b and the boolean. 2 is to put in 2 points in form of tuples. 3 is to put in a point and a vector. My code so far:
class line:
def __init__(self, a, b, noSlope):
self.a = a
self.b = b
self.noSlope = noSlope
def lineFromPoints(point1, point2):
deltaX = point2[0] - point1[0]
deltaY = point2[1] - point1[1]
if deltaX == 0:
return line(point1[0], 0, True)
else:
a = deltaY / deltaX
b = point1[1] - a * point1[0]
return line(a, b, False)
def lineFromVector(vector, point):
if vector[0] == 0:
return line(point1[0], 0, True)
else:
a = vector[1] / vector[0]
b = point1[1] - a * point1[0]
return line(a, b, False)
Not sure if there's a better way to do this