Skip to content

Instantly share code, notes, and snippets.

@akarsh1995
Created May 20, 2020 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akarsh1995/29b287d201819d2b39eb939dd8b386fa to your computer and use it in GitHub Desktop.
Save akarsh1995/29b287d201819d2b39eb939dd8b386fa to your computer and use it in GitHub Desktop.
Using dataclass decorator only python>=3.7
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