I was just reading about the 'unexpected result of is operator' which happens because Python cache numbers between -5 and 256.
This was discussed here: "is" operator behaves unexpectedly with integers
and here: "is" and "id" in Python 3.5
When I run one of the examples given there, I get different results between Python Idle and Python IDE (I'm using Jetbrains Pycharm professional edition - 5.0.4).
When using Python IDLE this is the result:
a = 1000
b = 1000
print (a is b) # prints False
when using Pycharm 5.0.4 this is the result:
a = 1000
b = 1000
print (a is b) # prints True
how could this be? I've rechecked, and my project's Python-Interpreter is exactly the same in both cases (both are Python 3.5.1). Not sure if this is something I've done wrong, and I was hoping if someone could explain this.
Edit:
I know 'a' is 'b' == true iff id(a) == id(b), and that you can check it like some of you mentioned in the comments. Perhaps I should have been more clear, what I don't understand is how could it be that an IDE has different behavior? I thought (and please, correct me, as it seems I'm wrong) that an IDE is just a user-friendly environment that uses external compilers / interpreters, and this is why these are independent of those IDE's (for instance, pycharm supports not only Python, and I could run Eclipse with C compiler, or Java etc. (all of which are not parts of the IDE).
Thanks, Alon.
From the documentation for the is operator:
Now lets check IDLE:
PyCharm:
In PyCharm both
a
andb
are the same objects when in IDLE they are not.Now what's instersting in PyCharm, that if you entering your code line by line, like in IDLE, you'll get the same results as in IDLE:
My guess, that
is optimized to:
So that's why you got same object for
a
andb
In python,
That's because we are matching the id(a) to id(b).
Consider,
a is b
would be False; your assumptions about identity only hold in CPython for numbers in the range-5 to 256
inclusive, which are singletons for performance reasons, but all other ints are recreated as needed, not singletons.Based on : reference
This is because of how
LOAD_CONST
byte code works:Since integers are stored as constants then assignments to the same integer in the same context will yield the exact same result, we can see that the arguement to
LOAD_CONST
is0
for both a and b:where as in an interactive session each command is compiled separately (so that they can be executed separately) so the constants will be different:
Similarly the constant in a function will always be the same but it will be different to the constant in other functions:
Also note that as @AniMenon has pointed out the numbers from -5 to 256 are singletons for optimization so the same will not hold true for numbers in that range.