The is
operator compares the identity while the ==
operator compares the value. Essentially x is y
is the same as id(x) == id(y)
+1
A:
WoLpH
2010-03-10 18:11:26
+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
2010-03-10 18:11:41
+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
2010-03-10 18:32:16