下面的代码返回的答案是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()
不计算对象引用的对象的大小。