String Identity Test

Roy Smith roy at panix.com
Tue Nov 1 10:31:59 EST 2005


Duncan Booth  <duncan.booth at suttoncourtenay.org.uk> wrote:
> If 'a!=b' then it will also be the case that 'a is not b'

That's true for strings, and (as far as I know), all pre-defined
types, but it's certainly possible to define a class which violates
that.

class isButNotEqual:
    def __ne__ (self, other):
        return True

a = isButNotEqual()
b = a
print "a != b:", a != b
print "a is not b:", a is not b


frame:play$ ./eq.py
a != b: True
a is not b: False

On the other hand, I can't imagine any reason why you would want to
define such a class, other than as a demonstration (or part of an
obfuscated Python contest).



More information about the Python-list mailing list