is there a difference between one line and many lines

C

Chris Angelico

Hello, I'm a newbie.
What's the defference between

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
 
C

Chris Angelico

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
 
D

Daniel Kluev

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.
.... a=-6; b=-6; c = a is b
.... return c.... a=-6
.... b=-6
.... c = a is b
.... return c2 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_VALUE2 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.
 
P

Peter Otten

vino19 said:
Hello, I'm a newbie.
What's the defference between


?

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:
.... b = -6
.... """True
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top