[Python源码剖析]字符缓冲池intern机制

static PyStringObject *characters[UCHAR_MAX + 1];
...

/* This dictionary holds all interned strings.  Note that references to
   strings in this dictionary are *not* counted in the string's ob_refcnt.
   When the interned string reaches a refcnt of 0 the string deallocation
   function will delete the reference from this dictionary.

   Another way to look at this is that to say that the actual reference
   count of a string is:  s->ob_refcnt + (s->ob_sstate?2:0)
*/

  Python源码[Object/stringobject.c]中UCHAR_MAX = 255.

所以Python解释器会缓冲256个字符串, 第257个字符串多次赋值不同的变量名, id()查看的结果就不同了:

>>> len(locals().keys())
4
>>> for i in range(256):
...     locals()['a%s'%i]= str(i)
...
>>> len(locals().keys())
261
>>> a='247'
>>> id(a247)
4301254088
>>> id(a)
4301235264
>>>

  可见intern机制可以缓存256个PyStringObject对象. 前三章看完了, 就主要两个知识点, 小整数集合范围[-5, 257), 字符缓冲池intern机制缓存256即0xff个字符串.

转载于:https://www.cnblogs.com/sigai/p/7442095.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值