Paul Rubin wrote: > Steven D'Aprano <st...@pearwood.info> writes: >> It is never >> correct to avoid using "is" when you need to compare for identity. > > When is it ever necessary to compare for identity?
Is that a trick question? The obvious answer is, any time you need to. The standard library has dozens of tests like: something is None something is not None Various standard modules include comparisons like: if frame is self.stopframe: if value is not __UNDEF__: if b is self.exit: if domain is Absent: if base is object: if other is NotImplemented: if type(a) is type(b): although that last one is probably better written using isinstance() or issubclass(). I have no doubt that there are many more examples. Comparing by identity are useful for interning objects, for testing that singletons actually are singletons, for comparing functions with the same name, for avoiding infinite loops while traversing circular data structures, and for non-destructively testing whether two mutable objects are the same object or different objects. -- Steven -- http://mail.python.org/mailman/listinfo/python-list