📜  如何从Python的地址获取值?

📅  最后修改于: 2022-05-13 01:55:49.446000             🧑  作者: Mango

如何从Python的地址获取值?

在本文中,我们将讨论如何从Python的地址获取值。首先,我们必须计算变量或Python对象的内存地址,这可以通过使用 id()函数来完成。

句法:

其中,python_object 是任何Python变量或数据结构,如列表、元组集等。

示例:获取特定Python对象的内存地址的Python程序



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}))


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)


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("____________________________________")


输出:

93937093504096
140292920620368
140292954508752
140292954711056

现在我们有了地址,我们可以使用 ctypes 模块再次从内存地址中获取 value/ Python对象。

ctypes 代表兼容数据类型,它允许调用 DLL 或共享库中的函数。它可用于将这些库包装在纯Python。它用于使用内存地址获取值/ Python对象

句法:

在哪里,

  • memeory_address 是变量的内存地址
  • value 是用于提取值的方法

示例 1:从内存地址访问值的Python程序

蟒蛇3

#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)

输出:



Actual value - 20
Memory address -  93937093504096
Value -  20

实施例Python程序以获得从Python数据结构的存储器地址的值

蟒蛇3

#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("____________________________________")

输出: