id()
id(object) Returns:
int · Added in v3.0 · Updated March 13, 2026 · Built-in Functions identity object memory built-in
The id() function returns the unique identity integer of an object. This integer corresponds to the object memory address in the CPython implementation. Two objects that exist simultaneously will have different identity values, while the same object will return the same identity value whenever id() is called on it.
Syntax
id(object)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
object | object | — | Any Python object (function, class, instance, list, etc.) |
Examples
Basic usage with different objects
# Integer objects
x = 42
print(id(42)) # 10756080
print(id(x)) # 10756080 (same as id(42))
# List objects
a = [1, 2, 3]
b = [1, 2, 3]
print(id(a)) # 140360991247616
print(id(b)) # 140360989501760 (different object, different id)
print(a is b) # False (they are different objects)
Same object reference
# Same object, same id
original = [1, 2, 3]
copy = original
print(id(original)) # 140360991247616
print(id(copy)) # 140360991247616 (same as original)
print(original is copy) # True
# After assignment, both variables point to the same object
Object lifecycle and id reuse
class MyClass:
pass
obj = MyClass()
first_id = id(obj)
print(f"Object id: {first_id}")
# Delete the object
del obj
# Create a new object of the same class
new_obj = MyClass()
new_id = id(new_obj)
print(f"New object id: {new_id}")
# Different objects have different ids
print(first_id == new_id) # False
Common Patterns
Checking object identity with is
The is operator compares object identity using id() under the hood:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True - same object
print(a is c) # False - different objects, same contents
# This is equivalent to:
print(id(a) == id(b)) # True
print(id(a) == id(c)) # False
Object interning for small integers
Python caches small integers (-5 to 256), so they always return the same id:
a = 100
b = 100
print(id(a) == id(b)) # True - same cached object
c = 257
d = 257
print(id(c) == id(d)) # False - not cached, different objects
Using id as dictionary key
Since id() returns a hashable integer, it can serve as a unique key:
tracked_objects = {}
class Widget:
def __init__(self, name):
self.name = name
w1 = Widget("Button")
w2 = Widget("Label")
tracked_objects[id(w1)] = w1
tracked_objects[id(w2)] = w2
print(id(w1) in tracked_objects) # True
print(tracked_objects[id(w1)].name) # Button
Integer caching gotchas
Python automatically caches small integers, which can lead to unexpected behavior:
# Small integers are cached
x = 5
y = 5
print(x is y) # True - both refer to the same cached object
# Larger integers are not cached
x = 1000
y = 1000
print(x is y) # False - different objects in memory
# This applies to strings and some other immutable types too
s1 = "hello"
s2 = "hello"
print(s1 is s2) # True (string interning)
Errors
id() itself rarely raises errors. Any object can be passed to it:
# Works with any object
print(id(None)) # Identity of None
print(id("string")) # Identity of a string
print(id([1, 2])) # Identity of a list
print(id(lambda: x)) # Identity of a function
# Even custom objects
class Foo:
pass
print(id(Foo)) # Identity of the class itself
print(id(Foo())) # Identity of an instance
See Also
- built-in::hash — Return hash value of an object
- built-in::type — Return the type of an object
- Python Docs: object identity — Official documentation on object identity