Skip to content

Commit a1d6c12

Browse files
committed
reimplement __hash__ with tuple; closes #1
1 parent 59818e0 commit a1d6c12

File tree

4 files changed

+9
-160
lines changed

4 files changed

+9
-160
lines changed

11-pythonic-obj/vector2d_v3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
8080
>>> v1 = Vector2d(3, 4)
8181
>>> v2 = Vector2d(3.1, 4.2)
82-
>>> hash(v1), hash(v2)
83-
(7, 384307168202284039)
82+
>>> hash(v1) != hash(v2)
83+
True
8484
>>> len(set([v1, v2]))
8585
2
8686
@@ -122,7 +122,7 @@ def __eq__(self, other):
122122
return tuple(self) == tuple(other)
123123

124124
def __hash__(self):
125-
return hash(self.x) ^ hash(self.y)
125+
return hash((self.x, self.y))
126126

127127
def __abs__(self):
128128
return math.hypot(self.x, self.y)

11-pythonic-obj/vector2d_v3_prophash.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@
8181
8282
>>> v1 = Vector2d(3, 4)
8383
>>> v2 = Vector2d(3.1, 4.2)
84-
>>> hash(v1), hash(v2)
85-
(7, 384307168202284039)
84+
>>> hash(v1) != hash(v2)
85+
True
8686
>>> len(set([v1, v2]))
8787
2
8888
@@ -131,7 +131,7 @@ def __eq__(self, other):
131131

132132
# BEGIN VECTOR_V3_HASH
133133
def __hash__(self):
134-
return hash(self.x) ^ hash(self.y)
134+
return hash((self.x, self.y))
135135
# END VECTOR_V3_HASH
136136

137137
def __abs__(self):

11-pythonic-obj/vector2d_v3_slots.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
7979
>>> v1 = Vector2d(3, 4)
8080
>>> v2 = Vector2d(3.1, 4.2)
81-
>>> hash(v1), hash(v2)
82-
(7, 384307168202284039)
81+
>>> hash(v1) != hash(v2)
82+
True
8383
>>> len(set([v1, v2]))
8484
2
8585
@@ -129,7 +129,7 @@ def __eq__(self, other):
129129
return tuple(self) == tuple(other)
130130

131131
def __hash__(self):
132-
return hash(self.x) ^ hash(self.y)
132+
return hash((self.x, self.y))
133133

134134
def __abs__(self):
135135
return math.hypot(self.x, self.y)

16-op-overloading/vector2d_v3.py

Lines changed: 0 additions & 151 deletions
This file was deleted.

0 commit comments

Comments
 (0)