is there a difference between one line and many lines

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

  1. vino19

    vino19 Guest

    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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. 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
    #3
  4. vino19

    Daniel Kluev Guest

    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)

    2 0 LOAD_CONST 1 (-6)
    3 STORE_FAST 0 (a)
    6 LOAD_CONST 1 (-6)
    9 STORE_FAST 1 (b)
    12 LOAD_FAST 0 (a)
    15 LOAD_FAST 1 (b)
    18 COMPARE_OP 8 (is)
    21 STORE_FAST 2 (c)
    3 24 LOAD_FAST 2 (c)
    27 RETURN_VALUE
    >>> dis.dis(test3)

    2 0 LOAD_CONST 1 (-6)
    3 STORE_FAST 0 (a)
    3 6 LOAD_CONST 1 (-6)
    9 STORE_FAST 1 (b)
    4 12 LOAD_FAST 0 (a)
    15 LOAD_FAST 1 (b)
    18 COMPARE_OP 8 (is)
    21 STORE_FAST 2 (c)
    5 24 LOAD_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
    #4
  5. vino19

    Peter Otten Guest

    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
    #5
  6. 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
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,612
  2. Jack
    Replies:
    9
    Views:
    2,739
  3. Replies:
    1
    Views:
    486
    Jonathan Mcdougall
    Dec 6, 2005
  4. vino19
    Replies:
    3
    Views:
    226
    Jean-Michel Pichavant
    Apr 21, 2011
  5. Peter Otten
    Replies:
    0
    Views:
    208
    Peter Otten
    Jan 17, 2013
Loading...

Share This Page