[英]Why does empty string show a size of 49 in python?
import sys
s = ""
print(sys.getsizeof(s))
output : 49 输出:49
What is the reason for this? 这是什么原因呢?
Because strings in Python are a certain type of object, they are not just a collection of enough characters to hold the string. 由于Python中的字符串是某种对象,因此它们不仅仅是容纳字符串的足够字符的集合。 As per the Python documentation,
sys.getsizeof()
will ... 根据Python文档,
sys.getsizeof()
将...
Return the size of an object in bytes.
返回对象的大小(以字节为单位)。
The size of the object may be larger for a variety of reasons, such as holding extra space for expansion without re-allocation, or having a minimum size. 出于各种原因,对象的大小可能会更大,例如,保留额外的扩展空间而无需重新分配,或者具有最小大小。 See, for example,
PEP393
introduced in Python 3.3, which shows several (verbose) structures. 例如,
PEP393
Python 3.3中引入的PEP393
,它显示了几种(详细的)结构。
If you want the length of the string rather than the size of the internal object, I'm sure there's a function for that. 如果您要的是字符串的长度 ,而不是内部对象的大小,那么我确定有一个函数。 Maybe it's called
len()
:-) 也许它叫做
len()
:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.