How to get value from address in Python ? (original) (raw)
Last Updated : 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
ctypes
val
=
20
print
(
id
(val))
print
(
id
([
1
,
2
,
3
,
4
,
5
]))
print
(
id
((
1
,
2
,
3
,
4
,
5
)))
print
(
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
ctypes
val
=
20
print
(
"Actual value -"
, val)
x
=
id
(val)
print
(
"Memory address - "
, x)
a
=
ctypes.cast(x, ctypes.py_object).value
print
(
"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
ctypes
val
=
[
1
,
2
,
3
,
4
,
5
]
print
(
"Actual value -"
, val)
x
=
id
(val)
print
(
"Memory address - "
, x)
a
=
ctypes.cast(x, ctypes.py_object).value
print
(
"Value - "
, a)
print
(
"____________________________________"
)
val
=
(
1
,
2
,
3
,
4
,
5
)
print
(
"Actual value -"
, val)
x
=
id
(val)
print
(
"Memory address - "
, x)
a
=
ctypes.cast(x, ctypes.py_object).value
print
(
"Value - "
, a)
print
(
"____________________________________"
)
val
=
{
1
,
2
,
3
,
4
,
5
}
print
(
"Actual value -"
, val)
x
=
id
(val)
print
(
"Memory address - "
, x)
a
=
ctypes.cast(x, ctypes.py_object).value
print
(
"Value - "
, a)
print
(
"____________________________________"
)
val
=
{
'id'
:
1
,
"name"
:
"sravan kumar"
,
"address"
:
"kakumanu"
}
print
(
"Actual value -"
, val)
x
=
id
(val)
print
(
"Memory address - "
, x)
a
=
ctypes.cast(x, ctypes.py_object).value
print
(
"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’}
____________________________________