-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
55 lines (39 loc) · 1.16 KB
/
main.py
File metadata and controls
55 lines (39 loc) · 1.16 KB
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
t1 = (1, 2, 3)
print(f"{hash(t1)}")
t2 = (1, 2, 3)
print(f"{hash(t2)}")
if hash(t1) == hash(t2):
print("t1 and t2 have equal hashes")
if t1 == t2:
print("t1 is equal to t2")
# l1 = [1, 2, 3]
# TypeError: unhashable type: 'list'
# print(hash(l1))
# t3 = (1, 2, 3, [1, 2, 3])
# TypeError: unhashable type: 'list'
# print(hash(t3))
class Koochooloo:
# The __hash__() method should return an integer.
# The only required property is that objects which
# compare equal have the same hash value;
# it is advised to mix together the hash values of the components
# of the object that also play a part in comparison
# of objects by packing them into a tuple and hashing the tuple.
def __hash__(self):
return 1378
def __eq__(self, _):
return True
k1 = Koochooloo()
k2 = Koochooloo()
print(f"k1 id: {id(k1)}")
print(f"k2 id: {id(k2)}")
print(f"k1 has: {hash(k1)}")
print(f"k1 has: {hash(k2)}")
# seems python used both hash and eq
# for dictionary and set keys
# remove __eq__ method to see k1 and k2
# are actually different under dict
kdict = {}
kdict[k1] = 10
kdict[k2] = 30
print(f"kset after adding k1 and k2: {kdict}")