is there a difference between one line and many lines

Steven D'Aprano steve+comp.lang.python at
Thu Apr 21 08:13:38 EDT 2011

On Thu, 21 Apr 2011 02:55:52 -0700, vino19 wrote:

> Sure, I understand that "is" is not "==", cause "is" just compares
> id(a)==id(b).
> I have a win32 CPython and the range of "singletons" is from -5 to 256
> on my machine.
> I am asking about what happens in Python interpreter? Why is there a
> difference between running one line like "a=1;b=1" and two lines like
> "a=1 \n b=1"? Does it decide to locate memory in different types depend
> on a code?

It's an implementation detail which is free to change from one version to 
the next. You can't rely on that behaviour, it could change at any time. 
You would have to study the source code of the exact version you are 
running, including any IDE if you are using an IDE, and see what it is 

The point is that Python is free to re-use immutable objects, or not re-
use them, as it sees fit.

>>> sys.version
'2.6.4 (r264:75706, Feb  1 2010, 13:33:07) \n[GCC 4.1.2 20070925 (Red Hat 
>>> a = 1.23; b = 1.23; a is b
>>> c = 1.23
>>> c is a


>>> a = 1001; b = 10001; a is b


More information about the Python-list mailing list