[Python-Dev] Re: redefining is

Casey Duncan casey at zope.com
Fri Mar 19 13:54:07 EST 2004


On Fri, 19 Mar 2004 19:31:16 +0100
Thomas Heller <theller at python.net> wrote:

> Barry Warsaw <barry at python.org> writes:
> 
> > On Fri, 2004-03-19 at 12:59, Michael Chermside wrote:
> >
> >>   Identity Objects can be (meaningfully) compared by:
> >> 
> >>     * Object Identity
> >>         To see if these are "the same object". Also useful
> >>         for low-level memory stuff according to Tim.
> >>         (just use '==', although 'is' would work too)
> >
> > Using == for identity objects is the wrong thing.  We should
> > discourage tests like "if obj == None" in favor of "if obj is None".
> 
> The problem (if there is any) with 'is' is that it exposes
> implementation details, therefore it should not be used unless on
> really knows what one is doing.
> 
> And 'if obj is None' gains performance by relying on one of these.
> 
> So I would consider 'if obj == None' correct, but unoptimized code.

The problem is that 'obj == None' is not the assertion you want to make
usually. 'obj == None' means "obj claims it is equal to None", whereas
'obj is None' means 'obj is the None object'. The latter is a much more
stringent assertion than the former which relies on the particular
implementation of obj.

-Casey




More information about the Python-Dev mailing list