Every time a Python object is stored in memory, it is assigned an integer value that references its address. The address is used to distinguish between different objects present in memory.
We will discuss using the id() function to get the memory address of Python variables as integer values and in hexadecimal format.
Printing Object Memory Address Using id() Function
The general syntax for the function is
id(obj),
where obj can be any Python object – integer, string, float, dictionary, list, tuple, function, etc.
Let’s see some examples.
1 2 3 4 5 6 7 8 9 10 |
a = 188 print(id(a)) # 140232668717328 b = 200 print(id(b)) # 140232668717712 c = -245 print(id(c)) # 140232668110608 d = -245 print(id(d)) # 140232668110608 e = "San Diego" print(id(e)) # 140232667897520 |
Note 1: Your output will likely differ because the Python interpreter randomly assigns memory addresses to objects.
Note 2: The ID assigned to an object is unique and remains constant for the current interpreter session. Wherever you re-execute the code, you get different ID values.
“Two objects with non-overlapping lifetimes may have the same id() value.” – Source: documentation.
Discussion
Notice that the difference between the id(a) and id(b) in the above code is 384. Why is that?
200-188 = 12, and each integer value is 32 bits (4 bytes), meaning 12 takes 12*32=384 bits which is the exact difference between the id(a) and id(b).
That means that Python stored the two integers next to each other in the memory. Python does this to make the memory allocation process efficient.
Another thing to note in the code above is that the identity values for equal integers are the same, id(c) and id(d). This is especially true for small integers but may not always be true.
Python does this for caching purposes by using pointers to the same memory location for variables with the same value.
Cases when IDs are Identical and when they are not
As shown in the code example above, there are cases when Python assigns the same identification number to multiple objects. Let’s discuss that more.
In most cases, different variables assigned the same values will have the same identity (ID) number (as shown in the example above), provided the objects in question are immutable.
Immutable objects are those that can’t be changed after creation. Immutable python objects include string, int, float, boolean, and tuple. On the other hand, mutable objects include lists, sets, and dictionaries.
Let’s see some examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Lists - e1 and e2 are mutable # Even if e1 and e2 have the same values, they will have # different IDs. e1 = ["item1", 44, "book"] print(id(e1)) # 139782525961600 e2 = ["item1", 44, "book"] print(id(e2)) # 139782525564480 # Tuples - f1 and f2 are immutable # In most cases, when values are the same # immutable values have the same ID values. f1 = ("item1", 44, "book") print(id(f1)) # 139782524577152 f2 = ("item1", 44, "book") print(id(f2)) # 139782524577152 |
You can also use the id() function to get the identification number of user-defined classes and functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# User-defined function. def sum1(a, b): return a + b a = sum1(a=1, b=2) print(id(a)) # 140609978833264 b = sum1(1, 2) print(id(b)) # 140609978833264 c = sum1(-6, 8) print(id(c)) # 140609978833232 # User-defined class class Sum1(object): def __init__(self, a, b): self.a = a self.b = b def sum_func(self): return self.a + self.b a = Sum1(1, 2) print(id(a)) # 140609976419856 b = Sum1(1, 2) print(id(b)) # 140609976420048 |
Function calls with the same argument values give the same ID values, whereas different class instances yield different identity values even with the same argument.
Getting object address in hexadecimal format
We can convert the integer memory address obtained using id() into a hexadecimal format using the hex() function. Here is an example.
1 2 3 4 |
a1 = "Poland" print(hex(id(a1))) # 0x7ffae8d2c4b0 a2 = 254 print(hex(id(a2))) # 0x7ffae8eb9950 |
Conclusion
This article discussed getting the memory address of a given object using the id() function. We also discussed cases when the IDs of different objects are the same. Lastly, we covered how to convert integer memory addresses into hexadecimal.