["python","integer","operators","identity","python-internals"]
Why does Python's "is" operator behave unexpectedly with integers?

Why does the following behave unexpectedly in Python? >>> a = 256 >>> b = 256 >>> a is b # This produces an expected result True >>> a = 257 >>> b = 257 >>> a is b # Why is this False? What is happening here? False >>> 257 is 257 # Yet the literal numbers compare properly True I am using Python 2.5.2. Trying some different versions of Python, it appears that Python 2.3.3 shows the above behaviour between 99 and 100. Based on the above, I can hypothesize that Python is internally implemented such that "small" integers are stored in a different way than larger integers and the is operator can tell the difference. Why the leaky abstraction? What is a better way of comparing two arbitrary objects to see whether they are the same when I don't know in advance whether they are numbers or not?
Take a look at this: >>> a = 256 >>> b = 256 >>> id(a) == id(b) True >>> a = 257 >>> b = 257 >>> id(a) == id(b) False Here's what I found in the documentation for "Plain Integer Objects": The current implementation keeps an array of integer objects for all integers between -5 and 256. When you create an int in that range you actually just get back a reference to the existing object. So, integers 256 are identical, but 257 are not. This is a CPython implementation detail, and not guaranteed for other Python implementations.