tags:

views:

160

answers:

4
+1  A: 

The is operator compares the identity while the == operator compares the value. Essentially x is y is the same as id(x) == id(y)

WoLpH
+4  A: 

The == operator tests for equality

The is keyword tests for object identity; whether we are talking about the same object. Note that multiple variables may refer to the same object.

Justin Ethier
A: 

See here

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value

asdfg
+1  A: 

For implementation reasons, "odp" is a bad example, but you should not use is unless you want the possibility of two identical strings to evaluate to false:

>>> lorem1 = "lorem ipsum dolor sit amet"
>>> lorem2 = " ".join(["lorem", "ipsum", "dolor", "sit", "amet"])
>>> lorem1 == lorem2
True
>>> lorem1 is lorem2
False

As others have said, is tests identity, not equality. In this case, I have two separate strings with the same contents. However, you should not depend on this either:

>>> odp1 = "odp"
>>> odp2 = "".join(["o", "d", "p"])
>>> odp1 == odp2
True
>>> odp1 is odp2
True

In other words, you should never use is to compare strings.

jcdyer