What does python sys getsizeof for string return?

前端 未结 2 1815
抹茶落季
抹茶落季 2021-01-03 11:48

What does sys.getsizeof return for a standard string? I am noticing that this value is much higher than what len returns.

2条回答
  •  独厮守ぢ
    2021-01-03 12:27

    To quote the documentation:

    Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.

    Built in strings are not simple character sequences - they are full fledged objects, with garbage collection overhead, which probably explains the size discrepancy you're noticing.

提交回复
热议问题