Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. class Point(object):
  2.  
  3.     POINT_ID = 0 # Class Attribute
  4.  
  5.     def __init__(self, x=0, y=0):
  6.         self.x = x
  7.         self.y = y
  8.         self.point_id = Point.POINT_ID
  9.         Point.POINT_ID += 1 #Increment class attribute each time an instance of the class is created
  10.  
  11.     def __add__(self, other): #Overload the '+' operator
  12.         newx = self.x + other.x
  13.         newy = self.y + other.y
  14.         return Point(newx, newy)
  15.  
  16.     def __iadd__(self, other): #Overload the '+=' operator
  17.         z = self + other
  18.         self.x = z.x
  19.         self.y = z.y
  20.         return self
  21.  
  22.     def __gt__(self, other): #Overload the '>' operator (you get '<' for free!)
  23.         if self.x > other.x and self.y > other.y:
  24.             return True
  25.         return False
  26.  
  27.     def __len__(self): #Overload the len() function
  28.         return ((self.x - 0)**2+(self.y-0)**2)**0.5
  29.  
  30.     def __str__(self): #You can now print your points
  31.         return "({}, {})".format(self.x, self.y)
  32.  
  33.  
  34. def main():
  35.     p1 = Point(1, 2)
  36.     p2 = Point(3, 4)
  37.     p3 = p1 + p2
  38.     p1 += p2
  39.     print(p3)
  40.     print(p1)
  41.     print(p1 > p2)
  42.     print(len(p1))
  43.  
  44. if __name__=="__main__":
  45.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement