Fix the Identity Bug
Advanced · Fix the bug · from How Python Works Under the Hood: Memory, GIL, and Bytecode
From the lesson: How Python Works Under the Hood: Memory, GIL, and Bytecode
Every time you type x = 42, a lot happens behind the scenes. Python creates an integer object in memory, assigns a reference to it, and manages its lifecycle automatically. Understanding these internals doesn't just satisfy curiosity -- it helps you write faster code, debug memory issues, and nail technical interviews.
Read the lessonThe code below uses is to compare values, which is unreliable. Fix it to use proper value comparison so that all three comparisons print True.
a = 1000
b = 10 ** 3
print(a is b) # Unreliable! May be True or False
name1 = 'hello world'
name2 = 'hello world'
print(name1 is name2) # Unreliable!
list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(list1 is list2) # Always False for lists
More Internals exercises
- Disassemble a FunctionAdvanced
- Predict Integer CachingAdvanced
- Count ReferencesAdvanced
- Slots vs Dict MemoryAdvanced
All Python exercises · 50 practice problems · Advanced Python quiz