Sunday, November 17, 2024
Google search engine
HomeLanguagesHow to get value from address in Python ?

How to get value from address in Python ?

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
import ctypes
  
# variable declaration
val = 20
  
# get the memory address of the python 
# object for variable
print(id(val))
  
# get the memory address of the python 
# object for list
print(id([1, 2, 3, 4, 5]))
  
# get the memory address of the python 
# object for tuple
print(id((1, 2, 3, 4, 5)))
  
# get the memory address of the python 
# object for set
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
import ctypes
  
# variable declaration
val = 20
  
# display variable
print("Actual value -", val)
  
# get the memory address of the python object 
# for variable
x = id(val)
  
# display memory address
print("Memory address - ", x)
  
# get the value through memory address
a = ctypes.cast(x, ctypes.py_object).value
  
# display
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
import ctypes
  
  
# variable declaration
val = [1, 2, 3, 4, 5]
  
# display variable
print("Actual value -", val)
  
# get the memory address of the python object 
# for variable list
x = id(val)
  
# display memory address
print("Memory address - ", x)
  
# get the value through memory address
a = ctypes.cast(x, ctypes.py_object).value
  
# display
print("Value - ", a)
  
print("____________________________________")
  
# variable declaration
val = (1, 2, 3, 4, 5)
  
# display variable
print("Actual value -", val)
  
# get the memory address of the python object
# for variable tuple
x = id(val)
  
# display memory address
print("Memory address - ", x)
  
# get the value through memory address
a = ctypes.cast(x, ctypes.py_object).value
  
# display
print("Value - ", a)
  
print("____________________________________")
  
# variable declaration
val = {1, 2, 3, 4, 5}
  
# display variable
print("Actual value -", val)
  
# get the memory address of the python object 
# for variable set
x = id(val)
  
# display memory address
print("Memory address - ", x)
  
# get the value through memory address
a = ctypes.cast(x, ctypes.py_object).value
  
# display
print("Value - ", a)
  
print("____________________________________")
  
# variable declaration
val = {'id': 1, "name": "sravan kumar", "address": "kakumanu"}
  
# display variable
print("Actual value -", val)
  
# get the memory address of the python object 
# for variable dictionary
x = id(val)
  
# display memory address
print("Memory address - ", x)
  
# get the value through memory address
a = ctypes.cast(x, ctypes.py_object).value
  
# display
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’}

____________________________________

RELATED ARTICLES

Most Popular

Recent Comments