Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. class Vector:
  2. def __init__(self, x, y):
  3. self.x = x
  4. self.y = y
  5. def __str__(self): # строчное представление
  6. return "Vector({},{})".format(self.x, self.y)
  7. def __add__(self, other): # +
  8. return Vector(self.x + other.x, self.y + other.y)
  9. def __iadd__(self, other): # +=
  10. self.x += other.x
  11. self.y += other.y
  12. def __mul__(self, other): # *
  13. if isinstance(other, (int, float)):
  14. return Vector(self.x * other, self.y * other)
  15. if isinstance(other, Vector):
  16. return self.x * other.x + self.y * other.y
  17. else:
  18. raise TypeError("неправильный тип")
  19. def __imul__(self, other):
  20. if isinstance(other, (int, float)):
  21. self.x *= other
  22. self.y *= other
  23. else:
  24. raise TypeError("lol")
  25. def __sub__(self, other): # -
  26. if isinstance(other, Vector):
  27. return Vector(self.x - other.x, self.y - other.y)
  28. def __isub__(self, other): # -=
  29. if isinstance(other, Vector):
  30. self.x -= other.x
  31. self.y -= other.y
  32.  
  33. class Point:
  34. def __init__(self, x, y):
  35. self.x = x
  36. self.y = y
  37. def __str__(self):
  38. return "Point({},{})".format(self.x, self.y)
  39. '''
  40. a = Vector(3,2)
  41. b = Vector(1,1)
  42. p1 = Point(1,1)
  43. print(a + b)
  44. print(randint(1,10))
  45. '''
  46. help(isinstance)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement