String Identity Test

Terry Reedy tjreedy at udel.edu
Thu Mar 5 11:36:28 EST 2009


Terry Reedy wrote:
> Hendrik van Rooyen wrote:
>> "S Arrowsmith" <si...intbox.UUCP> wrote:
>>
>>> "Small" integers get a similar treatment:
>>>
>>>>>> a = 256
>>>>>> b = 256
>>>>>> a is b
>>> True
>>>>>> a = 257
>>>>>> b = 257
>>>>>> a is b
>>> False
>>
>> This is weird - I would have thought that the limit
>> of "small" would be at 255 - the biggest number to fit in a byte.  256 
>> takes two bytes, so it must be

Ints take as least 4 bytes.  It is commonness of usage that determined 
caching.  The range was expanded a few years ago in anticipation of the 
new bytes type, whose contents are ints, not chars.

>> an arbitrary limit - could have been set at 300,
>> or 30 000...
> 
> 'Small' also goes to -10 or so.  256 was included, at minuscule cost, 
> because it is a relatively common number, being the number of bytes.

In fact, 3.0.1 starts with 36 internal references to the cached int 256!

 >>> import sys
 >>> sys.getrefcount(256)
38 # -2 for the function call

 >>> sys.getrefcount(257)
2

 >>> [sys.getrefcount(i)-2 for i in range(258)]

shows that only 15 cached ints start with more references. 0 has the 
most with 724 (and that small actually goes to -5).

tjr




More information about the Python-list mailing list