easy question on parsing python: "is not None"

Rhodri James rhodri at wildebst.demon.co.uk
Fri Aug 6 01:15:38 CEST 2010


On Thu, 05 Aug 2010 17:07:53 +0100, wheres pythonmonks  
<wherespythonmonks at gmail.com> wrote:

> Well, I am not convinced of the equivalence of not None and true:
>
>>>> not None
> True
>>>> 3 is True;
> False
>>>> 3 is not None
> True
>>>>

You're not testing for equivalence there, you're testing for identity.   
"is" and "is not" test whether the two objects concerned are (or are not)  
the same object.  Two objects can have the same value, but be different  
objects.  The interpreter can fool you by caching and reusing objects  
which have the same value when it happens to know about it, in particular  
for small integers, but this is just a happy accident of the  
implementation and in no way guaranteed by the language.  For example:

>>> "spam, eggs, chips and spam" is "spam, eggs, chips and spam"
True
>>> a = "spam, eggs, chips and spam"
>>> b = "spam, eggs, chips and spam"
>>> a is b
False
>>> a == b
True

Also, remember that "is not" is a single operator, *not* the concatenation  
of "is" and "not".  Your last test is probably not checking what you think  
it is :-)

>>> 3 is (not None)
False

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list