Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #3777

Re: is there a difference between one line and many lines

References <336b2051-5ecb-4b50-af24-2f9181448822@glegroupsg2000goo.googlegroups.com>
Date 2011-04-21 19:47 +1000
Subject Re: is there a difference between one line and many lines
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.689.1303379230.9059.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

is there a difference between one line and many lines vino19 <vinograd19@gmail.com> - 2011-04-21 02:38 -0700
  Re: is there a difference between one line and many lines Chris Angelico <rosuav@gmail.com> - 2011-04-21 19:47 +1000
  Re: is there a difference between one line and many lines Daniel Kluev <dan.kluev@gmail.com> - 2011-04-21 21:17 +1100
  Re: is there a difference between one line and many lines Peter Otten <__peter__@web.de> - 2011-04-21 12:39 +0200
  Re: is there a difference between one line and many lines Westley Martínez <anikom15@gmail.com> - 2011-04-21 07:16 -0700

csiph-web