How to get the memory address of an object in Python
In Python, everything is an object, from variables to lists and dictionaries everything is treated as objects. In this article, we are going to get the memory address of an object in Python.
Method 1: Using id()
We can get an address using the id() function. id() function gives the address of the particular object.
where the object is the data variables.
Example: Python program to get the address of different objects
Method 2: Using c_int, addressof modules
ctypes is a foreign function library for Python. It provides C compatible data types and allows calling functions in DLLs or shared libraries.
where object is the data variables
Example: Python program to get the memory address of a variable
It is also possible to get memory addresses in hexadecimal format. Here we will call the hex(address) function, to convert the memory address to hexadecimal representation.
- hex() is the memory hexadecimal representation to the address
- id is used to get the memory of the object
- object is the data
Example: Python program to get the memory address in hexadecimal representation.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course