is there a difference between one line and many lines

Chris Angelico rosuav at gmail.com
Thu Apr 21 05:47:07 EDT 2011


On Thu, Apr 21, 2011 at 7:38 PM, vino19 <vinograd19 at gmail.com> 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



More information about the Python-list mailing list