python查看内存地址的内容_python中如何查看指定内存地址的内容
python中一般并不需要查看內存內容,但作為從C/C++過來的人,有的時候還是想看看內存,有時是為了驗證內容是否與預期一致,有時是為了探究下內存布局。
from sys import getsizeof
from ctypes import string_at
'''
getsizeof(...)
getsizeof(object, default) -> int
Return the size of object in bytes.
string_at(ptr, size=-1)
string_at(addr[, size]) -> string
Return the string at addr.
'''
getsizeof用于獲取對象占用的內存大小,string_at用于獲取指定地址、指定字節長度的內容,因為返回的對象類型是bytes,可以調用hex()函數轉換成16進制查看。
對int對象的內存內容如下,首先通過函數id獲取對象的內存地址。
i = 100
type(i)
# int
s = string_at(id(i), getsizeof(i))
type(s)
# bytes
s
# b'>\x00\x00\x00\x00\x00\x00\x00\xa0\x99\xfd\x1d\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00d\x00\x00\x00'
s.hex()
# '3e00000000000000a099fd1d000000000100000
總結
以上是生活随笔為你收集整理的python查看内存地址的内容_python中如何查看指定内存地址的内容的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 变量不合法的表达式JAVA_Java8中
- 下一篇: java .net des_DES加密解
