如何在python中实现str?
发布时间:2020-12-20 11:27:43 所属栏目:Python 来源:网络整理
导读:import sys sys.getsizeof("")40 为什么空字符串由如此多的字节组成? 有人知道这40个字节中存储了什么吗? 解决方法 在Python中,字符串是对象,因此值是对象本身的大小.所以这个大小总是比字符串大小本身大. 来自stringobject.h: typedef struct { PyObject
>>> import sys >>> sys.getsizeof("") 40 为什么空字符串由如此多的字节组成? 解决方法
在Python中,字符串是对象,因此值是对象本身的大小.所以这个大小总是比字符串大小本身大.
来自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个字节;>(…) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |