Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > is there a difference between one line and many lines

Reply
Thread Tools

is there a difference between one line and many lines

 
 
vino19
Guest
Posts: n/a
 
      04-21-2011
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


?
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      04-21-2011
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
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      04-21-2011
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
 
Reply With Quote
 
Daniel Kluev
Guest
Posts: n/a
 
      04-21-2011
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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-21-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


 
Reply With Quote
 
Westley Martínez
Guest
Posts: n/a
 
      04-21-2011
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: is there a difference between one line and many lines vino19 Python 3 04-22-2011 12:25 AM
Re: is there a difference between one line and many lines vino19 Python 3 04-21-2011 10:39 AM
How to display a string in many lines, each lines have a specified length thuyptt@dsp.com.vn C++ 1 12-06-2005 07:26 AM
Asp.Net Calender, how to display 5 lines if there are only 5 lines in one month? Jack ASP .Net 9 10-12-2005 03:44 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57