【问题标题】:Why does sys.getsizeof() return a value that is 33 bytes more than the actual size?为什么 sys.getsizeof() 返回的值比实际大小大 33 个字节?
【发布时间】:2020-09-21 11:53:25
【问题描述】:

以下代码返回 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】:

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

    【讨论】:

    猜你喜欢
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    相关资源
    最近更新 更多