字符串对象PyStringObject
Python字符串对象PyStringObject
astr = 'astr'
id(astr)
Out[22]: 59244376L
astr += 'another'
id(astr)
Out[24]: 59947360Ltypedef struct {
Py_ssize_t ob_refcnt; // 引用计数
struct _typeobject *ob_type; // 类型指针
Py_ssize_t ob_size; // 字符串的长度,不计算C语言中的结尾NULL
long ob_shash; // 字符串的hash值,没有计算则为-1
int ob_sstate; // 字符串对象的状态: 是否interned等
char ob_sval[1]; // 保存字符串的内存,默认先分配1个字符,用来保存额外的末尾NULL值
/* 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;字符串的interned
字符串对象的回收
字符串对象的其他操作
最后更新于