-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
57 lines (41 loc) · 1.34 KB
/
Copy pathexample.py
File metadata and controls
57 lines (41 loc) · 1.34 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
56
57
"""Identity vs equality — is, ==, hashable, and immutable types."""
a = [1, 2, 3]
b = [1, 2, 3]
c = a
small_a = 256
small_b = 256
large_a = 257
large_b = 257
class Person:
def __init__(self, name):
self.name = name
def __eq__(self, other):
return isinstance(other, Person) and self.name == other.name
class HashablePerson:
def __init__(self, name):
self.name = name
def __eq__(self, other):
return isinstance(other, HashablePerson) and self.name == other.name
def __hash__(self):
return hash(self.name)
if __name__ == "__main__":
print("=== == vs is ===")
print(f"a == b: {a == b}")
print(f"a is b: {a is b}")
print(f"a is c: {a is c}")
print("\n=== Integer interning (implementation detail) ===")
print(f"small 256 is: {small_a is small_b}")
print(f"large 257 is: {large_a is large_b}")
print("\n=== None, True, False — always use is ===")
value = None
print(value is None)
print("\n=== Hashable vs unhashable ===")
try:
{a: "list key"}
except TypeError as e:
print(f" list not hashable: {e}")
p = HashablePerson("Alice")
print(f" set of HashablePerson: {{{p}}}")
print("\n=== Custom __eq__ without __hash__ ===")
p1, p2 = Person("Bob"), Person("Bob")
print(f" equal: {p1 == p2}, same id: {p1 is p2}")