"is" and ==

Erik Max Francis max at alcyone.com
Wed May 30 01:57:42 EDT 2007


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 && max 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



More information about the Python-list mailing list