Print Memory Address of Python Variable

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.

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.

You can also use the id() function to get the identification number of user-defined classes and functions.

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.

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.