Surprise using the 'is' operator

Terry Reedy tjreedy at udel.edu
Wed Sep 27 13:14:44 EDT 2006


"tobiah" <toby at tobiah.org> wrote in message 
news:451A9824.6040106 at tobiah.org...
> wesley chun wrote:
>>> "Terry Reedy" <tjr... at udel.edu> wrote:
>>> > as a side note, the ints that are cached (for current versions of
>>> > Python) are in range(-1, 100)... is this documented somewhere?
>>> Not true for at least 2.4 and 2.5.  The cached range has expanded

> I don't pretend to understand the reasons for all of this,
> but if it is possible to use the same integer object whenever
> it is referenced, then why should there be a limit on range?

Preallocating all the 4 billion 4-byte ints would overfill the memory of 
most machines ;-)

So there is a tradeoff between startup time, required minimal space usage 
by *every* program, needed or not, and runtime savings of time and space by 
programs that do make frequent use of 'small' ints.  The expansion from 99 
or 100 to 257 is partly a look ahead to a new 'bytes' type for 3.0 and 
maybe 2.x.

> 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?

Yes

>  Why should more be made,
> when they all do the same thing, and are not subject to change?

Because such a situation is a special case and the delivered code is for 
everyone.  If you compile your own binary, you can change the #define XXX 
257 (where XXX is something obvious like MAX_SMALL_INT) in intobject.c to 
65535 to make a special-purpose binary for such a use as you describe.

Terry J. Reedy






More information about the Python-list mailing list