Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different behavior in python script and python idle?

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.

like image 904
WKPlus Avatar asked Jul 07 '15 06:07

WKPlus


Video Answer


1 Answers

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:

  • "is" operator behaves unexpectedly with integers
  • What's with the Integer Cache inside Python?
like image 178
poke Avatar answered Oct 17 '22 10:10

poke