Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Point(object):
- POINT_ID = 0 # Class Attribute
- def __init__(self, x=0, y=0):
- self.x = x
- self.y = y
- self.point_id = Point.POINT_ID
- Point.POINT_ID += 1 #Increment class attribute each time an instance of the class is created
- def __add__(self, other): #Overload the '+' operator
- newx = self.x + other.x
- newy = self.y + other.y
- return Point(newx, newy)
- def __iadd__(self, other): #Overload the '+=' operator
- z = self + other
- self.x = z.x
- self.y = z.y
- return self
- def __gt__(self, other): #Overload the '>' operator (you get '<' for free!)
- if self.x > other.x and self.y > other.y:
- return True
- return False
- def __len__(self): #Overload the len() function
- return ((self.x - 0)**2+(self.y-0)**2)**0.5
- def __str__(self): #You can now print your points
- return "({}, {})".format(self.x, self.y)
- def main():
- p1 = Point(1, 2)
- p2 = Point(3, 4)
- p3 = p1 + p2
- p1 += p2
- print(p3)
- print(p1)
- print(p1 > p2)
- print(len(p1))
- if __name__=="__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement