Difference between 'is' and '=='

Roy Smith roy at panix.com
Mon Apr 3 10:37:11 EDT 2006


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.



More information about the Python-list mailing list