Difference between 'is' and '=='

Joel Hedlund joel.hedlund at gmail.com
Tue Mar 28 11:35:31 CEST 2006

>>You should _never_ use 'is' to check for equivalence of value. Yes, due
>>to the implementation of CPython the behaviour you quote above does
>>occur, but it doesn't mean quite what you seem to think it does.
> /me not checking for value. I'm checking for identity. Suppose "a" is a
> constant. I want to check if "b" is the same constant.

/me too. That's what my example was all about. I was using identity to a known 
CONSTANT (in caps as per python naming conventions :-) to sidestep costly value 
equality computations.

> By doing an "is" instead of a "==" you *can* catch some errors.
> <snip>
> By
> testing with "is" you test for *that* integer, the one defined on your
> module and that shouldn't go out of it anyway.

I totally agree with you on this point. Anything that helps guarding against 
"stealthed" errors is a good thing by my standards.

/Joel Hedlund

More information about the Python-list mailing list