Python id() function

Updated on Jan 07, 2020


The id() function returns a unique numeric identifier associated with the object.

In standard Python (i.e CPython) the identifier represents the memory address of the object. Although, this may vary in other implementation.

The unique identifier spring into existing when you define an object and will not change until the program is running. We can use this identifier to determine whether two objects are same or not.

The syntax of id() function is as follows:

id(obj) -> unique identifier

Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
>>> 
>>> a = 10
>>> 
>>> b = 5
>>> 
>>>
>>> id(a), id(b)
(10919712, 10919552)
>>> 
>>> 
>>> a = b # a now references same object as b
>>> 
>>>
>>> id(a), id(b)
(10919552, 10919552)
>>>
a = 10
b = 5 

print(id(a), id(b))

a = b # a now references same object as b

print(id(a), id(b))


Initially, variables a and b references two distinct objects. As a result, id() call returns two unique identifiers. Next, we assign the object b to a. Now, a and b references the same object (5). Thus, the next id() call returns the same identifier.


Other Tutorials (Sponsors)