为什么 sys.getsizeof() 返回的值比实际大小大 33 个字节?



以下代码返回 35 作为答案:

print(sys.getsizeof((1024).to_bytes(2, byteorder='big')))

int.to_bytes(( 不应该返回 1024 的字节表示形式,长度为 2 个字节,因为文档提到:

The integer is represented using length bytes. An OverflowError is raised if the integer is not representable with the given number of bytes.

我理解错了什么?

我相信您忽略了在创建时每个python对象附带的垃圾收集器开销。另外请注意,sys.getsizeof()不计算对象引用的对象的大小。

最新更新