"is" and ==

BlueJ774 jaysherby at gmail.com
Wed May 30 01:59:33 EDT 2007


On May 30, 12:57 am, Erik Max Francis <m... at alcyone.com> wrote:
> BlueJ774 wrote:
> > Can someone please explain to me the difference between the "is"
> > keyword and the == boolean operator.  I can't figure it out on my own
> > and I can't find any documentation on it.
>
> > I can't understand why this works:
>
> >     if text is None:
>
> > and why this always returns false:
>
> >     if message is 'PING':
>
> > even when message = 'PING'.
>
> > What's the deal with that?
>
> `x is y` means the same thing as:
>
>         id(x) == id(y)
>
> You use the `is` operator for when you're testing for _object identity_,
> not value.  `None` is a special object sentinel that is not only a value
> but a special _object_, and so if you're testing whether or not an
> object is `None`, you do so with the `is` operator.
>
> If you're testing whether an object is equal to the string "PING" then
> you do not want to do so by identity, but rather value, so you use the
> `==` operator, not `is`.
>
> --
> Erik Max Francis && m... at alcyone.com &&http://www.alcyone.com/max/
>   San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
>    You could have another fate / You could be in another place
>     -- Anggun

Thanks.  That's exactly what I was looking for.




More information about the Python-list mailing list