【发布时间】:2022-01-10 12:06:38
【问题描述】:
CPython中的字符串结构:
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;
ob_shash 成员包含哈希,我了解它的用法。但是ob_sstate 存储字符串是否被实习的信息。我浏览了很多函数,但找不到这个结构字段的使用位置?它是干什么用的?
【问题讨论】:
-
PyStringObjectshould be the Python 2 string type,这已经严重过时了。对于 Python 3,应该只有PyUnicodeObject/PyCompactUnicodeObject/PyASCIIObject和PyBytesObject。你问的是什么版本? -
哦,什么,我没有被告知。我说的是 3.9 版常规 ascii 字符串(如果您在交互式解释器中键入“hello world”,则会出现)。无论如何,我也很乐意为 python 2 字符串提供答案。
-
@MisterMiyagi 那么你碰巧知道 Python 2 字符串的答案吗?
-
抱歉,我对爬过这么老的代码库没什么兴趣。
标签: python c cpython python-internals internals