# is there a difference between one line and many lines

Discussion in 'Python' started by vino19, Apr 21, 2011.

1. ### vino19Guest

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

?

vino19, Apr 21, 2011

2. ### Chris AngelicoGuest

On Thu, Apr 21, 2011 at 7:38 PM, 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

You may want to use the == operator rather than "is". When you use
"is", you're asking Python if the two variables are referencing the
exact same object, but with ==, you're asking if they're equivalent.
With integers from -1 to 99, Python keeps singletons, which means that
your test would work if you used 6 instead of -6; but there's no
guarantee of anything with the negatives. However, it doesn't matter
whether the variables are pointing to the same object or not, if you
use ==, because two different objects holding the number -6 will
compare equal.

Hope that clarifies it!

Chris Angelico

Chris Angelico, Apr 21, 2011

3. ### Chris AngelicoGuest

On Thu, Apr 21, 2011 at 7:55 PM, 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?

Ah okay! In that case, I'm guessing this is going to be an oddity of
the IDLE system, because it's compiling each line separately. When you
put it on a single line, it's saving some trouble by sharing the
constant; when you do them separately, it doesn't optimize like that.

Chris Angelico

Chris Angelico, Apr 21, 2011
4. ### Daniel KluevGuest

On Thu, Apr 21, 2011 at 8:38 PM, vino19 <> wrote:
> Hello, I'm a newbie.
> What's the defference between
>*skip*

What is version of CPython?
In 2.7.1 and 3.1.3 both versions return True, and moreover, are
compiled to identical bytecode.

>>> def test1():

.... a=-6; b=-6; c = a is b
.... return c
>>> def test3():

.... a=-6
.... b=-6
.... c = a is b
.... return c
>>> test1()

True
>>> test3()

True
>>> dis.dis(test1)

3 STORE_FAST 0 (a)
9 STORE_FAST 1 (b)
18 COMPARE_OP 8 (is)
21 STORE_FAST 2 (c)
27 RETURN_VALUE
>>> dis.dis(test3)

3 STORE_FAST 0 (a)
9 STORE_FAST 1 (b)
18 COMPARE_OP 8 (is)
21 STORE_FAST 2 (c)
27 RETURN_VALUE

So AFAIK, there is no difference for interpreter itself, its purely
syntactic, and is compiled to exactly same bytecode.

--
With best regards,
Daniel Kluev

Daniel Kluev, Apr 21, 2011
5. ### Peter OttenGuest

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

Peter Otten, Apr 21, 2011
6. ### Westley MartínezGuest

On Thu, Apr 21, 2011 at 02:38:52AM -0700, 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

>
> ?
> --
> http://mail.python.org/mailman/listinfo/python-list

Depends on how the interpreter was implemented.

Westley Martínez, Apr 21, 2011