在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