Surprise using the 'is' operator

Georg Brandl g.brandl-nospam at gmx.net
Wed Sep 27 14:29:24 EDT 2006


Terry Reedy wrote:
> "Georg Brandl" <g.brandl-nospam at gmx.net> wrote in message 
> news:efe7v1$8ls$1 at news.albasani.net...
>> tobiah wrote:
>>> Suppose I fill an list with 100 million random integers in the range
>>> of 1 - 65535.  Wouldn't I save much memory if all of the ocurrances
>>> of '12345' pointed to the same integer object?  Why should more be made,
>>> when they all do the same thing, and are not subject to change?
>>
>> Because for typical usage of integers (which doesn't include your 
>> example),
>> it is more expensive to check if there's already an integer with that 
>> specific
>> value out there than to create a new one.
> 
> I am not sure what you are saying.  Every (C) int is checked to see if it 
> is in the preallocated range before making a new Python int object.  The 
> reason for the low limit is the various time-space tradeoffs applied across 
> the entire range of programs.

Maybe I phrased that misleadingly (expensive refering to memory space). In the
end, I wanted to say the same thing you did: it's a tradeoff between space and
time.

Georg



More information about the Python-list mailing list