Storing and checking a boolean in a python class

Viewed 2609

So I was trying to create an object which basically has a constructor that takes two coordinates, xcoord and ycoord. I further created methods which shift the coordinates and I have to check if the point is valid or not(criteria for validity is if the coordinates are out of a specified range it should return False else True).

Problem:

My class only returns the validity for the initial points, not the shifted points.

What do I need to correct my code?

Code:

class Point:
    MaxScreenSize=10
    def __init__(self,x,y):
        self.xcoord=x
        self.ycoord=y
        if 0>self.xcoord or self.xcoord>Point.MaxScreenSize or 0>self.ycoord or self.ycoord>Point.MaxScreenSize:
            Point.isValidPt=False
        else:
            Point.isValidPt=True
    def translateX(self,shiftX):
        self.xcoord=self.xcoord+shiftX
    def translateY(self,shiftY):
        self.ycoord=self.ycoord+shiftY

Test Code:

I tried my code and it only returns the isValidFunction variable for my initial points (gives me True instead of False for following code)

p=Point(9,2) 
p.translateX(20)
p.translateY(10)
p.isValidPt
2 Answers
Related