-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30_equals_and_is.py
More file actions
77 lines (56 loc) · 1.86 KB
/
30_equals_and_is.py
File metadata and controls
77 lines (56 loc) · 1.86 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# --------------------------
# == vs is
# --------------------------
# --- Rules ---
"""
== → Value equality → Checks if the contents/values are the same.
is → Identity equality → Checks if both variables point to the exact same object in memory.
1. is → identity check (same object in memory).
2. == → value equality (contents).
3. Use is for None checks: if x is None.
4. Don’t use is for strings/numbers unless you specifically want to check identity (rare).
"""
# 1. Basic Example
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True (values are same)
print(a is b) # False (different objects in memory)
# 2. is for Singletons (Special Case)
# Use is for unique objects like None, True, False.
x = None
print(x is None) # ✅ Correct
print(x == None) # Works, but not Pythonic
# 3. Immutable Types & Interning
# Python reuses some small immutable objects (like small integers & short strings) for efficiency.
# This means is might seem to work sometimes by coincidence.
a = 256
b = 256
print(a is b) # True (Python caches small ints)
a = 257
b = 257
print(a is b) # False (not cached)
# Same for short strings:
x = "hello"
y = "hello"
print(x is y) # True (string interning)
# But:
x = "hello world!"
y = "hello world!"
print(x is y) # Might be False (longer strings may not be interned)
# 4. Mutable Types
# For lists, dicts, sets — even if values are equal, is will almost always be False unless you explicitly assign one to another.
list1 = [1, 2]
list2 = [1, 2]
print(list1 == list2) # True
print(list1 is list2) # False
list3 = list1
print(list1 is list3) # True (same object)
# 5. Comparing Custom Objects
# By default, == uses is (identity) for custom objects unless you override __eq__.
class A:
pass
obj1 = A()
obj2 = A()
print(obj1 == obj2) # False (no __eq__ defined)
print(obj1 is obj2) # False (different objects)
print(obj1 is obj1) # True