Difference between 'is' and '=='

Dave Hansen iddw at hotmail.com
Mon Apr 3 10:48:09 EDT 2006


On 3 Apr 2006 10:37:11 -0400 in comp.lang.python, roy at panix.com (Roy
Smith) wrote:

>Adam DePrince  <adam.deprince at gmail.com> wrote:
>> It just happens that the
>>logical operation 
>>
>> (a is b ) -> (a == b )
>>
>>is always True.  
>
>Only for small values of "always".  You can always do pathological
>things with operators:
>
>class Foo:
>    def __eq__ (self, other):
>        return False
>
>f = Foo()
>print f is f
>print f == f
>
>frame:play$ ./is.py
>True
>False
>
>This may even be useful.  What if you were trying to emulate SQL's
>NULL?  NULL compares false to anything, even itself.  To test for
>NULLness, you have to use the special "is NULL" operator.

Another instance where this may be useful is IEEE-754 NaN.  I don't
have fpconst to verify if that's the case, but I would expect 
NaN is NaN to be true, but NaN == NaN to be false.

Regards,
                                        -=Dave

-- 
Change is inevitable, progress is not.



More information about the Python-list mailing list