[Python-Dev] sys.intern should work on bytes

Hrvoje Niksic hrvoje.niksic at avl.com
Mon Sep 23 10:03:12 CEST 2013


On 09/20/2013 06:50 PM, PJ Eby wrote:
> On Fri, Sep 20, 2013 at 9:54 AM, Jesus Cea <jcea at jcea.es> wrote:
>> Why str/bytes doesn't support weakrefs, beside memory use?
>
> The typical use case for weakrefs is to break reference cycles,

Another typical use case, and the prime reason why languages without 
reference counting tend to introduce weak references, is managing object 
caches with automatic disposal of otherwise unused items. Such a cache 
is rarely necessary for primitive objects, so Python's choice to spare 
memory for weakrefs is quite justified.

However, if one wanted to implement their own sys.intern(), inability to 
refer to strings would become a problem. This is one reason why 
sys.intern() directly fiddles with reference counts instead of reusing 
the weakref machinery. (The other of course being that intern predates 
weakrefs by many years.)



More information about the Python-Dev mailing list