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

Steve Holden steve at holdenweb.com
Mon Jan 26 14:44:43 CET 2009


Terry Reedy wrote:
> 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
> try:
>   False,True
> except NameError:
>   False,True = 0,1
> 
> to make code back compatible.
> 
I would claim that the ability to rebind True and False is a simple bug,
though one not likely to be fixed in an 2.x release. The code above
doesn't rebind True and False in interpreters that have them ...

> 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).
> 
And that was a bug too, in this case one that *was* removed in 2.4, I
believe. Don't have 2.3 lying around just now.

Python 2.4.3 (#1, May 24 2008, 13:47:28)
[GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> None = 3
SyntaxError: assignment to None

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list