-
Notifications
You must be signed in to change notification settings - Fork 32
/
lec11prob4.py
66 lines (50 loc) · 2.01 KB
/
lec11prob4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# lec11prob4.py
#
# Lecture 11 - Classes
# Problem 4
#
# edX MITx 6.00.1x
# Introduction to Computer Science and Programming Using Python
'''
Consider the following code from the last lecture video:
Your task is to define the following two methods for the Coordinate class:
1. Add an __eq__ method that returns True if coordinates refer to same point
in the plane (i.e., have the same x and y coordinate)
2. Define __repr__, a special method that returns a string that looks like
a valid Python expression that could be used to recreate an object with the
same value. In other words, eval(repr(c)) == c given the definition of
__eq__ from part 1.
For more on __repr__ see this SO post:
http://stackoverflow.com/questions/452300/python-object-repr-self-should-be-an-expression
'''
class Coordinate(object):
def __init__(self, x, y):
self.x = x
self.y = y
def getX(self):
# Getter method for a Coordinate object's x coordinate.
# Getter methods are better practice than just accessing an attribute directly
return self.x
def getY(self):
# Getter method for a Coordinate object's x coordinate.
return self.y
def __str__(self):
return '<' + str(self.getX()) + ', ' + str(self.getY()) + '>'
# returns True if coordinates refer to same point in the plan
def __eq__(self, other):
# check to see if 'other' is same type
assert type(other) == type(self)
if self.getX() == other.getX():
if self.getY() == other.getY():
return True
return False
# returns a string that looks like a valid Python expression that could be
# used to recreate an object with the same value.
def __repr__(self):
return 'Coordinate(' + str(self.getX()) + ', ' + str(self.getY()) + ')'
# create new object test_coord
test_coord = Coordinate(10, 20)
# call new methods to determine output
print(test_coord.__eq__(10, 20))
print(test_coord.__eq__(5, 10))
print(test_coord.__repr__())