python獲取記憶體中的資料

2021-10-12 09:14:33 字數 607 閱讀 5559

import ctypes

ctypes.cast(id(1), ctypes.py_object).value # 1

注:跨執行緒或跨ide不會成功,報 『segmentation fault』 錯誤

另外,理論上所有的記憶體位址資料都可以獲取到,也可寫入資料,比如multiprocessing.queue例項化物件…

在c/c++中,通過&獲取變數的記憶體位址,通過*獲取記憶體位址中的資料。

在python中,通過id獲取變數的記憶體位址,那如何通過記憶體位址獲取資料呢?

import ctypes

value = 'hello world' # 定義乙個字串變數

address = id(value) # 獲取value的位址,賦給address

get_value = ctypes.cast(address, ctypes.py_object).value # 讀取位址中的變數

print(address,get_value)

2390895579248 hello world

Python獲取Window的記憶體資料

python中執行cmd檢視windows記憶體使用情況 import os import requests,json 檢視window記憶體使用情況 def select memory 結果列表 memory list 輸入 cmd 命令 cmd systeminfo 執行 cmd 命令 res ...

Python獲取資料

1 raw input raw input 函式用來獲取從鍵盤上獲取一行文字,將以字串的形式返回使用者輸入的一切內容,意思就是比如從鍵盤輸入的是數字也將以字串的形式獲取。2 imput input 把讀入的資料預設為python表示式,對於input 函式,輸入字串時要用引號將其轉化為合法的pyth...

python 獲取物件 python中獲取物件資訊

拿到乙個變數,除了用 isinstance 判斷它是否是某種型別的例項外,還有沒有別的方法獲取到更多的資訊呢?例如,已有定義 class person object def init self,name,gender self.name name self.gender gender class s...