class Point:
def __init__(self, initX, initY):
\"\"\" Create a new point at the given coordinates. \"\"\"
self.x = initX
self.y = initY
Both getX
and getY
are methods in your code, not attributes. So you will need to call them using getX()
and getY()
.
So ma=(p2.getY-p1.getY)/(p2.getX-p1.getX)
becomes:
ma = (p2.getY()-p1.getY())/(p2.getX()-p1.getX())
And so on, the other code changes.
Otherwise, you can also define your methods as @property:
class Point:
...
...
@property
def getX(self):
return self.x
@property
def getY(self):
return self.y
...
And now you can access these as p1.getX
and p2.getY
and so on.
Note that the above @property decorator turns the method into a getter, which makes sense to use only with private variables (variables defined to start with _
).
As such, since both x and y are normal attributes of your class, you can access them directly without using and property decorators or using getter methods, like p1.x
and p2.y
, as @Padraic points in his post.