[Python-Dev] A proposal has surfaced on comp.lang.python to redefine "is"

Neal Norwitz neal at metaslash.com
Wed Mar 17 23:23:37 EST 2004


On Wed, Mar 17, 2004 at 03:45:32PM -0800, Guido van Rossum wrote:
> 
> It seems to me that 'is' should never be used for immutables except
> for singletons like None.  Perhaps PyChecker should warn about
> inappropriate use of 'is' to compare two immutable objects, unless one
> of them is None?

There currently is a check for using 'is' or 'is not' with any literal:

    def f(x):
      if x is 505: print "don't do that"

    def g(x):
      if x is 'xbc': print "don't do that"

    def h(x):
      if x is ('xbc',): print "don't do that"

    def i(x):
      if x is ['xbc']: print "don't do that"

    def none_test(x):
      if x is None: print "ok, fine, do that"

    def ok(x):
      a = ['xbc']
      if x is a: print "ok, fine, do that"

$ checker.py tt.py
tt.py:3: Using is 505, may not always work
tt.py:6: Using is xbc, may not always work
tt.py:9: Using is (Stack Item: (xbc, <type 'str'>, 1),), may not always work
tt.py:12: Using is [Stack Item: (xbc, <type 'str'>, 1)], may not always work

It's supposed to work with True/False, but there's a bug.  That, 
the print of StackItems, and many more. :-)

Neal



More information about the Python-Dev mailing list