Different behavior in Python script and Python IDLE?
Asked Answered
M

1

13

In the Python IDLE:

>>> a=1.1
>>> b=1.1
>>> a is b
False

But when I put the code in a script and run it, I will get a different result:

$cat t.py
a=1.1
b=1.1
print a is b
$python t.py
True

Why did this happen? I know that is compares the id of two objects, so why the ids of two objects are same/unique in Python script/IDLE?

I also found that, if I use a small int, for example 1, instead of 1.1, the result will be the same in both the Python script and Python IDLE. Why did small int and small float have different behavior?

I am using CPython 2.7.5.

Molten answered 7/7, 2015 at 6:7 Comment(2)
see: #15172195Leisaleiser
I believe that IDLE has nothing to do with the observed 2.7 difference in behavior of this code between Python's batch and interactive mode. If WKPlus had entered $python at the command line and entered the 3 lines in standard interactive (REPL) mode, I believe the answer would have been False, as it is with 3.11. If the script had been loaded in an IDLE editor window and run, I believe the answer would have been True, as it is today.Inexpugnable
P
10

When Python executes a script file, the whole file is parsed first. You can notice that when you introduce a syntax error somewhere: Regardless of where it is, it will prevent any line from executing.

So since Python parses the file first, literals can be loaded effectively into the memory. Since Python knows that these are constant, all variables that represent those constant values can point to the same object in memory. So the object is shared.

This works for ints and floats, but even for strings; even when there is a constant expression that needs to be evaluated first:

a = "foo"
b = "foo"
c = "fo" + "o"
print(a is b)
print(a is c)

Now in IDLE, the behavior is very different: As an interactive interpreter, IDLE executes every line separately. So a = 1.1 and b = 1.1 are executed in separated contexts which makes it impossible (or just very hard) to figure out that they both share the same constant literal value and could share the memory. So instead, the interpreter will allocate two different objects, which causes the identity check using is to fail.

For small integers, the situation is a bit different. Because they are often used, CPython stores a set of integers (in the range between -5 and 256) statically and makes that every value of these points to the same int object. That’s why you get a different result for small integers than for any other object. See also the following questions:

Papandreou answered 7/7, 2015 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.