is there a difference between one line and many lines

Steven D'Aprano steve+comp.lang.python at pearwood.info
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 
doing.

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 
4.1.2-27)]'
>>>
>>>
>>> a = 1.23; b = 1.23; a is b
True
>>> c = 1.23
>>> c is a
False


but:

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



-- 
Steven



More information about the Python-list mailing list