base.py 922 B

1234567891011121314151617181920212223242526272829
  1. # указываем, что можно либо int, либо float
  2. from typing import Union
  3. class PointError(Exception):
  4. pass
  5. class Point:
  6. def __init__(self, x: Union[int, float], y: Union[int, float]) -> None:
  7. if not isinstance(x, (int, float)) or not isinstance(y, (int, float)):
  8. raise PointError("x,y should be an integer type")
  9. self.x = x
  10. self.y = y
  11. def __add__(self, other: "Point") -> "Point":
  12. return Point(self.x + other.x, self.y + other.y)
  13. def __sub__(self, other: "Point") -> "Point":
  14. return Point(self.x - other.x, self.y - other.y)
  15. def __eq__(self, other: object) -> bool:
  16. if not isinstance(other, Point):
  17. raise NotImplementedError
  18. return self.x == other.x and self.y == other.y
  19. def distance_to(self, other: "Point") -> float:
  20. p = self - other
  21. return (p.x ** 2 + p.y ** 2) ** 0.5