-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy path__hash__.py
More file actions
36 lines (27 loc) · 764 Bytes
/
Copy path__hash__.py
File metadata and controls
36 lines (27 loc) · 764 Bytes
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
class ImmutablePoint:
def __init__(self, x, y):
self._x = x
self._y = y
@property
def x(self):
return self._x
@property
def y(self):
return self._y
def __hash__(self):
return hash((self._x, self._y))
def __eq__(self, other):
if not isinstance(other, ImmutablePoint):
return False
return self._x == other._x and self._y == other._y
if __name__ == '__main__':
ip = ImmutablePoint(10, 20)
ip2 = ImmutablePoint(10, 20)
print(ip.x, ip.y)
print(ip == ip2) # True
print(ip is ip2) # False
# Since they are hashable, we can also use these objects as keys in a dictionary
d = {ip: 'hello'}
print(d)
print(d[ip])
print(d[ip2])