"""A simple class for points.""" from math import isfinite, sqrt from typing import Final class Point: """ A class for representing a point in the two-dimensional plane. >>> p = Point(1, 2.5) >>> p.x 1 >>> p.y 2.5 >>> try: ... Point(1, 1e308 * 1e308) ... except ValueError as ve: ... print(ve) x=1 and y=inf must both be finite. """ def __init__(self, x: int | float, y: int | float) -> None: """ The constructor: Create a point and set its coordinates. :param x: the x-coordinate of the point :param y: the y-coordinate of the point """ if not (isfinite(x) and isfinite(y)): raise ValueError(f"x={x} and y={y} must both be finite.") #: the x-coordinate of the point self.x: Final[int | float] = x #: the y-coordinate of the point self.y: Final[int | float] = y def distance(self, p: "Point") -> float: """ Get the distance to another point. :param p: the other point :return: the distance >>> Point(1, 1).distance(Point(4, 4)) 4.242640687119285 """ return sqrt((self.x - p.x) ** 2 + (self.y - p.y) ** 2)