Last active
March 11, 2022 20:41
-
-
Save lewoudar/23423cbc58a8f4264aa82386c3e549e1 to your computer and use it in GitHub Desktop.
An example of argument validation without using pydantic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| from dataclasses import dataclass | |
| @dataclass | |
| class Point: | |
| x: int | |
| y: int | |
| def compute_distance(a: Point, b: Point) -> float: | |
| if not isinstance(a, Point): | |
| raise TypeError | |
| if not isinstance(b, Point): | |
| raise TypeError | |
| x = math.pow(a.x - b.x, 2) | |
| y = math.pow(a.y - b.y, 2) | |
| return math.sqrt(x + y) | |
| p1 = Point(1, 2) | |
| p2 = Point(3, 5) | |
| print('distance between p1 and p2 is:', compute_distance(p1, p2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment