sameness/identity

Markus Schaber markus at schabi.de
Mon Oct 1 17:49:23 EDT 2001


Hi,

Xavier Defrang <xavier at perceval.net> schrub:

>> Just to confuse you some more:
>> 
>> >>> a = 3
>> >>> b = 3
>> >>> a is b
>> 1
>> >>> a = 10000
>> >>> b = 10000
>> >>> a is b
>> 0
> 
> Damn, this is weird. :)
> After reading that, I wrote a small script :

[snip]

> That '100' definetly looks like some hardcoded limit, has anybody a
> clear
> explanation for this?  Why the interpreter behaves differently for
> numbers
> lower than 100 and the others?  I guess it's for optimization purpose
> since most algorithms deal with small numerical values but I'd be
> really happy if I knew what's really going on in here...

Yes, that's correct, and documented. The interpreter optimizes using a 
cache for the numbers -100 to 100. This is configurable and can be 
switched off. Also, e. G. identical strings and symbols in a module are 
shared.

As both are immutable, this shouldn't matter in most cases. But you 
have to be careful when using object identity on Strings.
markus

-- 
"The strength of the Constitution lies entirely in the determination of 
each citizen to defend it. Only if every single citizen feels duty 
bound to do his share in this defense are the constitutional rights 
secure." -- Albert Einstein



More information about the Python-list mailing list