Skip to content

Commit 9c16dff

Browse files
committed
Added Coordinate class in python
1 parent 8baaeb1 commit 9c16dff

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Classes_in_python.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# coordinates class in python
2+
3+
import math
4+
5+
class coordinates(object):
6+
def __init__(self,x,y):
7+
self.x = x
8+
self.y = y
9+
def distance(self,other):
10+
return math.sqrt(( self.x-other.x)**2 + (self.y-other.y))
11+
12+
13+
import math
14+
15+
def sq(x):
16+
return x*x
17+
18+
19+
class Coordinates(object):
20+
def __init__(self, x, y):
21+
self.x = x
22+
self.y = y
23+
def __str__(self):
24+
return "<"+str(self.x)+","+str(self.y)+">"
25+
def distance(self,other):
26+
return math.sqrt(sq(self.x - other.x)
27+
+ sq(self.y - other.y))
28+
29+
30+
31+
class Coordinate(object):
32+
def __init__(self, x, y):
33+
self.x = x
34+
self.y = y
35+
36+
def getX(self):
37+
# Getter method for a Coordinate object's x coordinate.
38+
# Getter methods are better practice than just accessing an attribute directly
39+
return self.x
40+
41+
def getY(self):
42+
# Getter method for a Coordinate object's y coordinate
43+
return self.y
44+
45+
def __eq__(self,other):
46+
return (self.x == other.x) and (self.y == other.y)
47+
48+
def __repr__(self):
49+
return "Coordinate("+str(self.x)+", "+str(self.y)+")"
50+
51+
def __str__(self):
52+
return '<' + str(self.getX()) + ',' + str(self.getY()) + '>'

0 commit comments

Comments
 (0)