3

Example 1:

➜  /tmp  cat t.py 
a = 250000000000
b = 250000000000
print id(a), id(b), id(a) == id(b)
➜  /tmp  python t.py 
140450848587992 140450848587992 True  #(Why is True?)

Example 2:

➜  /tmp  python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 250000000000
>>> b = 250000000000
>>> print id(a), id(b), id(a) == id(b)
140443481339400 140443481339208 False #(I think it should be False)

I know Python has a small integer cache pool(from -5 to 256), so two big integers should have different id all the time.

How to explain the different behaviours of big integer when run in Python shell and *.py file ?

2 Answers 2

3

If I were the Python interpreter reading the .py file (as in your first example), I'd allocate memory for this number only once and then make a and b point to this location.

For example, the interpreter reads this file and sees that two variables are assigned the same value and thinks: "Do I want to waste memory by allocating two portions for the same value? No, I don't. Instead, I'd better allocate one single chunk and store this value in it". So, there's only one copy of this value and that's why these variables have identical id's.

In the second case Python has to allocate memory when you do the assignment, so there are two pieces of memory with the same data.

To sum up, in the first case the interpreter knows much more about the code (the whole code is given), so it can optimize it (and generate .pyo files) while in the second one it simply can't do any optimization.

Sign up to request clarification or add additional context in comments.

Comments

2

Actually the CPython runtime does indeed "interns" (cache) some immutable objects (ints up to a certain value, strings that could be legal python identifiers etc) as an optimisation. All of this behaviour is totally implementation dependant and should not in any case be relied upon, as you just noticed.

1 Comment

Thanks, now i know this behaviour is totally implementation dependant !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.