How to get value from address in Python ?
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
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'}
____________________________________
Similar Reads
How to get the address for an element in Python array? In this article we are going to discuss about getting the address of an particular element in the Python array. In python we can create the array using numpy. Numpy stands for numeric python used to create and process arrays. We have to import numpy module import numpy as np Syntax to create array:
3 min read
Python script to get device vendor name from MAC Address Prerequisite: Python Requests A MAC address is a unique identity of a network interface that is used to address the device in a Computer Network. What does MAC Address consist of? A MAC address is made up of 12 hexadecimal digits, 6 octets separated by ':'. There are 6 octets in a MAC address. In th
2 min read
How to get Geolocation in Python? In this article, we will discuss on how to get Geolocation when you enter any location name and it gives all the useful information such as postal code, city, state, country etc. with the latitudes and the longitudes (the specific coordinates) and vice-versa in which we provide the coordinates to ge
2 min read
How to Access dict Attribute in Python In Python, A dictionary is a type of data structure that may be used to hold collections of key-value pairs. A dictionary's keys are connected with specific values, and you can access these values by using the keys. When working with dictionaries, accessing dictionary attributes is a basic function
6 min read
How to get the current username in Python When building interacting Python programs you might need to get the current username for features like personalizing user experience, providing user-specific resources, Implementing robust logging and debugging, strengthening security, etc. We will use different Python modules and functions like os.
4 min read