Python 3 reference · Built-in Functions
Python id() Function
Return an object's identity: a unique integer for the object while it exists. In CPython it is the memory address.
Python 3 Since 3.0
Syntax
id(object)
Parameters
| Parameter | Description |
|---|---|
object |
Any object. |
Return value
An int, unique among objects alive at the same time.
Examples
a = [1, 2]
b = a # the same list
c = [1, 2] # an equal but different list
print(id(a) == id(b), id(a) == id(c))
print(a is b, a is c, a == c)
Output
True False True False True
def add_item(items):
items.append("new")
return id(items)
shopping = []
print(add_item(shopping) == id(shopping), shopping)
Output
True ['new']
Good to know
Use is to compare identity rather than comparing id() values. Two objects with non-overlapping lifetimes
may get the same id().
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
hash()· Return the hash value of an object: an integer used by dicts and sets to find keys quickly. Only immutable values are hashable.type()· With one argument, return an object's type. With three arguments, create a new class dynamically.- Python Variables · tutorial