Possible Duplicate:
Is there any difference between “foo is None” and “foo == None”?
Quite a simple question really.
Whats the difference between:
if a.b is 'something':
and
if a.b == 'something':
excuse my ignorance
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first checks for identity the second for equality.
Examples:
The first operation using
ismay or may not result inTruebased on where these items, i.e., strings, are stored in memory.Checking, id() shows them stored at different locations.
The second operation (
==) will giveTruesince the strings are equal.Another example showing that
iscan beTrueorFalse(compare with the first example), but==works the way we’d expect:this implies that both of these short literals were stored in the same memory location unlike the two longer strings in the example above.
No surprise here, what we would have expected.
I believe
isuses id() to determine if the same objects in memory are referred to (see @SvenMarnach comment below for more details)