How to get value from address in Python ?

23 Aug, 2021
In this article, we will discuss how to get the value from the address in Python. First, we have to calculate the memory address of the variable or python object which can be done by using the id() function.
Syntax:
id(python_object)
where, python_object is any python variable or data structure like list, tuple set, etc.
Example: Python program to get the memory address of particular python objects
Python3
#import ctypesimport ctypes  # variable declarationval = 20  # get the memory address of the python # object for variableprint(id(val))  # get the memory address of the python # object for listprint(id([1, 2, 3, 4, 5]))  # get the memory address of the python # object for tupleprint(id((1, 2, 3, 4, 5)))  # get the memory address of the python # object for setprint(id({1, 2, 3, 4, 5}))
Output:
93937093504096
140292920620368
140292954508752
140292954711056
Now that we have addresses, we can get value/python objects again from the memory address using ctypes module.
ctypes stands for compatible data types, which allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python. It is used to get the value/Python objects using memory address
Syntax:
ctypes.cast(memory_address,ctypes.py_object).value
where,
memeory_address is the memory address of the variable
value is the method which is used to extract a value
Example 1: Python program to access the value from memory address
Python3
#import ctypesimport ctypes  # variable declarationval = 20  # display variableprint("Actual value -", val)  # get the memory address of the python object # for variablex = id(val)  # display memory addressprint("Memory address - ", x)  # get the value through memory addressa = ctypes.cast(x, ctypes.py_object).value  # displayprint("Value - ", a)
Output:
Actual value - 20
Memory address -  93937093504096
Value -  20
Example 2: Python program to get the value from memory address of python data structures
Python3
#import ctypesimport ctypes    # variable declarationval = [1, 2, 3, 4, 5]  # display variableprint("Actual value -", val)  # get the memory address of the python object # for variable listx = id(val)  # display memory addressprint("Memory address - ", x)  # get the value through memory addressa = ctypes.cast(x, ctypes.py_object).value  # displayprint("Value - ", a)  print("____________________________________")  # variable declarationval = (1, 2, 3, 4, 5)  # display variableprint("Actual value -", val)  # get the memory address of the python object# for variable tuplex = id(val)  # display memory addressprint("Memory address - ", x)  # get the value through memory addressa = ctypes.cast(x, ctypes.py_object).value  # displayprint("Value - ", a)  print("____________________________________")  # variable declarationval = {1, 2, 3, 4, 5}  # display variableprint("Actual value -", val)  # get the memory address of the python object # for variable setx = id(val)  # display memory addressprint("Memory address - ", x)  # get the value through memory addressa = ctypes.cast(x, ctypes.py_object).value  # displayprint("Value - ", a)  print("____________________________________")  # variable declarationval = {'id': 1, "name": "sravan kumar", "address": "kakumanu"}  # display variableprint("Actual value -", val)  # get the memory address of the python object # for variable dictionaryx = id(val)  # display memory addressprint("Memory address - ", x)  # get the value through memory addressa = ctypes.cast(x, ctypes.py_object).value  # displayprint("Value - ", a)  print("____________________________________")
Output:
Actual value – [1, 2, 3, 4, 5]
Memory address –  140292824840224
Value –  [1, 2, 3, 4, 5]
____________________________________
Actual value – (1, 2, 3, 4, 5)
Memory address –  140292853914128
Value –  (1, 2, 3, 4, 5)
____________________________________
Actual value – {1, 2, 3, 4, 5}
Memory address –  140292824862304
Value –  {1, 2, 3, 4, 5}
____________________________________
Actual value – {‘id’: 1, ‘name’: ‘sravan kumar’, ‘address’: ‘kakumanu’}
Memory address –  140292825009760
Value –  {‘id’: 1, ‘name’: ‘sravan kumar’, ‘address’: ‘kakumanu’}
____________________________________
Picked
python-basics
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.