>>> import sys
>>> sys.getsizeof("")
40
为什么空字符串使用这么多字节?有谁知道这40个字节中存储了什么?
在Python中,字符串是对象,因此值是对象本身的大小。所以这个大小总是大于字符串本身的大小。
From stringobject.h
:
typedef struct {
PyObject_VAR_HEAD
long ob_shash;
int ob_sstate;
char ob_sval[1];
/* Invariants:
* ob_sval contains space for 'ob_size+1' elements.
* ob_sval[ob_size] == 0.
* ob_shash is the hash of the string or -1 if not computed yet.
* ob_sstate != 0 iff the string object is in stringobject.c's
* 'interned' dictionary; in this case the two references
* from 'interned' to this object are *not counted* in ob_refcnt.
*/
} PyStringObject;
从这里您可以获得有关如何使用这些字节的一些线索:
-
len(str)+1
用于存储字符串本身的字节;
- 8 个字节用于哈希;
- (...)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)