Python Program to Find and Print Address of Variable
In this article, we are going to see how to find and print the address of the Python variable.
It can be done in these ways:
- Using id() function
- Using addressof() function
- Using hex() function
Method 1: Find and Print Address of Variable using id()
We can get an address using id() function, id() function gives the address of the particular object.
where, object is the data variables.
Here we are going to find the address of the list, variable, tuple and dictionary.
140234866534752 94264748411744 140234904267376 140234866093264
Method 2: Find and Print Address of Variable using addressof()
we can also get memory addresses using these functions, 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.
Method 3: Find and Print Address of Variable using hex()
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.
0x7fba9b0ae8c0 0x5572da858b60 0x7fba9f3c4a10 0x7fba9b05b8c0