is there a difference between one line and many lines

Peter Otten __peter__ at web.de
Thu Apr 21 12:39:02 CEST 2011


vino19 wrote:

> Hello, I'm a newbie.
> What's the defference between
> 
>>>>a=-6; b=-6; a is b
>>>>True
> 
> and
> 
>>>>a=-6
>>>>b=-6
>>>>a is b
>>>>False
> 
> ?

When you write it as a single line the assignments to a and b are part of 
the same compilation process, and as an optimization CPython's bytecode 
compiler looks for identical (integer, float, string) constants and uses the 
same object to represent them. To show that it's really the compilation not 
the number of lines:

>>> exec """a = -6
... b = -6
... """
>>> a is b
True





More information about the Python-list mailing list