在python中访问内存地址 [英] Access memory address in python
本文介绍了在python中访问内存地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是:如何在python中读取内存地址的内容? 例子: ptr = id(7) 我想读取ptr指向的内存内容. 谢谢.
My question is: How can I read the content of a memory address in python? example: ptr = id(7) I want to read the content of memory pointed by ptr. Thanks.
推荐答案
看看 ctypes.string_at .这是一个例子.它转储Python 3整数的原始数据结构.希望您只是在做练习.没有理由使用纯Python做到这一点.
Have a look at ctypes.string_at. Here's an example. It dumps the raw data structure of a Python 3 integer. Hopefully you're only doing this as an exercise. No reason to do this with pure Python.
from ctypes import string_at
from sys import getsizeof
from binascii import hexlify
a = 0x7fff
print(hexlify(string_at(id(a), getsizeof(a))))
输出
b'02000000d8191e1e01000000ff7f'
这篇关于在python中访问内存地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文