Created
May 20, 2020 18:12
-
-
Save akarsh1995/29b287d201819d2b39eb939dd8b386fa to your computer and use it in GitHub Desktop.
Using dataclass decorator only python>=3.7
This file contains 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
from dataclasses import dataclass | |
from math import sqrt | |
@dataclass | |
class Position: | |
x: float = 0.0 | |
y: float = 0.0 | |
def distance_to(self, other): | |
return sqrt((self.x - other.x)**2 + (self.y - other.y)**2) | |
# Let's define coordinate 0, 0 | |
p1 = Position() | |
# let's define coordinate 1, 1 | |
p2 = Position(x=1, y=1) | |
# Euclidean distance between coordinates | |
print('Distance between p1 and p2:', p1.distance_to(p2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment