Instead of “==”? I know what “is” is, it is comparing the identity of the variable. But when would you ever want to do that? All it has ever done for me is cause problems. After using it for a while (because I felt it made my code more readable), I am not declaring war on “is”.
Does anyone use it for something that “==” wouldn’t do? I don’t understand why they didn’t make ‘is’ the same thing as ‘==’, like they made ‘and’ the same as ‘&&’ etc. If someone wanted the pointer, they just have to say “id(x) == id(y)” and we wouldn’t have this confusion.
It’s one of the “gotcha’s” in python that I don’t understand, and trip a lot of newbies up. The reason I think it trips people up is that they don’t get why it would do an identity comparison. What is the point(er)?
Edit:
Thanks for the great answers. I think what new people should take away is “always use ‘==’ unless you know what you are doing”!
Yes, there is a reason.
When you want to compare object-identity (“same object”) and not object-equality (“same value”). In almost all cases
==(object-equality) is the correct operator to use. (As pointed out in the comments, the trivial case I skipped entirely is thex is Noneidiom —Noneis the sole inhabitant ofNoneType.)One case for object-identity is an identity-cache/dictionary which is a common pattern in some proxy or “inside-out” situations. In this case I don’t want the value for “a similar object” back, I want value for “the same object” back.
Happy coding.
Thinking about pointers in Python is wrong. Values in Python are objects and each value represents itself. That is,
x is yis only true whenxandyevaluate to the same object. When objects are passed in python this same concept holds — the object itself is passed. No need to think about references.Python is Call-by-Sharing/Call-by-Object-Sharing although the term “Call-By-Reference” — which exists in the “official documentation places” — is often incorrectly used to describe the behavior (perhaps to “ease” the transition from other languages where the term is also often incorrectly used).