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

问题描述 投票:0回答:1

下面的代码返回的答案是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-3.x byte sizeof
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.