Difference between 'is' and '=='
Erik Max Francis
max at alcyone.com
Mon Mar 27 17:25:23 EST 2006
Terry Reedy wrote:
> The Python specification allows but does not require such behind-the-scenes
> implementation optimization hacks. As released, CPython 2.4 caches -5 to
> 99, I believe. In 2.5, the upper limit was increased to 256. The limits
> are in a pair of #define statements in the int object source file. Anyone
> who compiles from source can adjust as desired (though the corresponding
> test will fail unless also adjusted ;-).
>
> I think the visibility of this implementation detail from Python code is an
> example of a leaky abstraction. For more, see
> http://www.joelonsoftware.com/articles/LeakyAbstractions.html
I don't see that as quite the same thing. That you can fiddle around
with the `is` operator to investigate how small integers are cached
doesn't really reveal any profound underlying abstraction, except that
maybe all Python entities are true objects and that integers are
immutable, which are things hopefully everyone was already aware of.
If you're trying to test integer equality, you should be using the `==`
operator, not the `is` operator, so what you find out about how things
are caching is really irrelevant.
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
And covenants, without the sword, are but words and of no strength to
secure a man at all. -- Thomas Hobbes, 1588-1679
More information about the Python-list
mailing list