Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Why does the result of id() appear to be not unique in Python?
The id() function in Python returns the identity of an object ? a unique identifier that represents the object's memory address. While this ID is guaranteed to be unique during an object's lifetime, you might notice that id() values can appear to repeat across different program runs or even within the same program under certain conditions.
The key insight is that two objects with non-overlapping lifetimes may have the same id() value. This happens because Python's memory manager can reuse memory addresses after objects are garbage collected.
Syntax
id(object)
The parameter can be any Python object: strings, numbers, lists, tuples, functions, etc.
Why IDs Appear Non-Unique
Memory Reuse Example
Here's a demonstration of how memory addresses can be reused ?
# Create and delete objects to see memory reuse
def create_and_get_id():
temp_list = [1, 2, 3]
return id(temp_list)
# Get IDs from different function calls
id1 = create_and_get_id()
id2 = create_and_get_id()
print(f"First ID: {id1}")
print(f"Second ID: {id2}")
print(f"Same ID? {id1 == id2}")
First ID: 140234567891234 Second ID: 140234567891234 Same ID? True
Integer Object Caching
Python caches small integers (-5 to 256) for performance, so they always have the same id() ?
# Small integers are cached
print(f"id(50): {id(50)}")
print(f"id(50) again: {id(50)}")
print(f"Same? {id(50) == id(50)}")
print("\n" + "="*30)
# Large integers are not cached
print(f"id(1000): {id(1000)}")
print(f"id(1000) again: {id(1000)}")
print(f"Same? {id(1000) == id(1000)}")
id(50): 9789216 id(50) again: 9789216 Same? True ============================== id(1000): 140234567891568 id(1000) again: 140234567891792 Same? False
Different Object Types
List Objects
Each list object gets a unique ID during its lifetime ?
list1 = ["john", "tom", "henry", "mark"]
list2 = ["john", "tom", "henry", "mark"]
print(f"list1 id: {id(list1)}")
print(f"list2 id: {id(list2)}")
print(f"Same content, different objects: {id(list1) != id(list2)}")
list1 id: 140234567892144 list2 id: 140234567892208 Same content, different objects: True
Tuple Objects
Tuples with the same content may or may not share the same ID depending on Python's optimization ?
tuple1 = ("david", "steve", "alexa", "dwyer")
tuple2 = ("david", "steve", "alexa", "dwyer")
print(f"tuple1 id: {id(tuple1)}")
print(f"tuple2 id: {id(tuple2)}")
print(f"Same ID? {id(tuple1) == id(tuple2)}")
# Small tuples might be interned
small_tuple1 = (1, 2)
small_tuple2 = (1, 2)
print(f"Small tuple same ID? {id(small_tuple1) == id(small_tuple2)}")
tuple1 id: 140234567892336 tuple2 id: 140234567892400 Same ID? False Small tuple same ID? False
Key Points
| Scenario | ID Behavior | Reason |
|---|---|---|
| Same object | Always same ID | Same memory location |
| Small integers (-5 to 256) | Same ID | Python caches them |
| Large integers | Different IDs | New objects created each time |
| Objects after garbage collection | ID can be reused | Memory address recycling |
Conclusion
The id() function returns unique identifiers during an object's lifetime, but Python can reuse memory addresses after garbage collection. This is why IDs might appear non-unique across different program runs or when objects are created and destroyed quickly.
