<var> is None vs. <var> == None

Terry Reedy tjreedy at udel.edu
Mon Jan 26 05:07:44 CET 2009

Roger wrote:
>> And, just for completeness, the "is" test is canonical precisely because
>> the interpreter guarantees there is only ever one object of type None,
>> so an identity test is always appropriate. Even the copy module doesn't
>> create copies ...
> Does the interpreter guarantee the same for False and True bools?

Yes.  Bool(x) should return one of the two existing instances.

In 2.x, the *names* 'True' and 'False' can be rebound because bool is 
new and people write
except NameError:
   False,True = 0,1

to make code back compatible.

In 3.0, the names are keywords, just like 'None' and cannot be rebound, 
so x is True is guaranteed to answer whether x *is* the true object.

Back before rebinding 'None' was prohibited, 'is None' was not 
completely guaranteed either (absent reading the rest of a file to be 
sure no rebinding would be done).

More information about the Python-list mailing list