Python: different results when using PyCharm and IDLE / python

I was just reading about the "unexpected result of the statement-operator", which is due to the fact that the Python cache numbers are from -5 to 256.

This has been discussed here: the "is" operator behaves unexpectedly with integers

and here: "is" and "id" in Python 3.5

When I run one of the examples here, I get different results between the Python Idle and the Python IDE (I use 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 double-checked, and my Python-Interpreter project is exactly the same in both cases (both Python 3.5.1). Not sure if this is what I did wrong, and I was hoping if anyone could explain it.

Edit:

I know that 'a' is 'b' == true iff id (a) == id (b), and you can check it, as some of you mentioned in the comments. Perhaps I should have been more clear that I do not understand, so it could be that the IDE has a different behavior? I thought (and please correct me, it seems to me that I am mistaken) that the IDE is just a user-friendly environment that uses external compilers / interpreters, and that is why they are not dependent on these IDEs (for example, support pycharm is not only Python, and I could run Eclipse with a C or Java compiler, etc. (all of which are not part of the IDE).

Thanks, Alon.

+7
python integer pycharm python-idle
source share
3 answers

This is due to how LOAD_CONST works:

co_consts[consti] stack.

Since integers are stored as constants, assigning the same integer in the same context gives the exact result, we can see that the argument LOAD_CONST is 0 for a and b:

 >>> import dis >>> dis.dis("a = 1000 ; b = 1000") 1 0 LOAD_CONST 0 (1000) 3 STORE_NAME 0 (a) 6 LOAD_CONST 0 (1000) 9 STORE_NAME 1 (b) 12 LOAD_CONST 1 (None) 15 RETURN_VALUE # ^ this is the argument 

where, as in an interactive session, each command is compiled separately (so that they can be executed separately), so the constants will be different:

 >>> code1 = compile("a = 1000","<dummy file>","exec") >>> code2 = compile("a = 1000","<dummy file>","exec") >>> code1.co_consts, code2.co_consts ((1000, None), (1000, None)) >>> code1.co_consts[0] is code2.co_consts[0] False 

Similarly, a constant in a function will always be the same, but it will differ from a constant in other functions:

 def f(): return 1000 def g(): return 1000 #different code object!! #these all work assert f() is f() assert g() is g() assert f() is not g() assert f() is not 1000 and g() is not 1000 

Also note that as @AniMenon it is pointed out that numbers from -5 to 256 are singleton for optimization, therefore this will not be true for numbers in this range.

+4
source share

From the documentation for is the operator :

The is and is not operators verify the identity of an object: x is y true if and only if x and y are the same object.

Now let's check IDLE:

 >>> a = 1000 >>> b = 1000 >>> print ( a is b ) False >>> >>> >>> id(a) 35334812 >>> id(b) 35334800 

PyCharm:

 >>> a = 1000 b = 1000 print (a is b) True >>> id(a) 36079236 >>> id(b) 36079236 

In PyCharm, both a and b are the same objects when they are not in IDLE.

Now, as prompted by PyCharm, if you enter your code line by line, as in IDLE, you will get the same results as in IDLE:

 >>> a = 1000 >>> b = 1000 >>> print (a is b) False 

My guess is that

 >>> a = 1000 b = 1000 

optimized for:

 >>> a = b = 1000 >>> print (a is b) True 

So why do you have the same object for a and b

+3
source share

is will return True if two variables point to the same object, == will return True if the objects referenced by the variables are equal.

In python

 >>> a = [1, 2, 3] >>> b = a >>> b is a True >>> b == a True >>> b = a[:] >>> b is a False >>> b == a True 

This is because we map id (a) to id (b).

Consider

 a = 1000 b = 1000 a is b 

a is b will be false; your identity assumptions are only saved in CPython for numbers in the range -5 to 256 inclusive, which are singles for performance reasons, but all other ints are recreated as needed, not single ones.

Based on: help

0
source share

All Articles