easy question on parsing python: "is not None"

Ethan Furman ethan at stoneleaf.us
Thu Aug 5 13:09:04 EDT 2010


Roald de Vries wrote:
> On Aug 5, 2010, at 5:42 PM, wheres pythonmonks wrote:
>> How does "x is not None" make any sense?  "not x is None" does make 
>> sense.
>>
>> I can only surmise that in this context (preceding is) "not" is not a
>> unary right-associative operator, therefore:
>>
>> x is not None === IS_NOTEQ(X, None)
>>
>> Beside "not in" which seems to work similarly, is there other
>> syntactical sugar like this that I should be aware of?
> 
> 'not None' first casts None to a bool, and then applies 'not', so 'x is 
> not None' means 'x is True'.

This is not correct.  'is not' is a single operator, and in this case is 
checking that the object 'x' is not the same as the object 'None'.  No 
boolean conversions are done -- False and True have nothing to do with 
this example.

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
--> x = 5
--> x is not None
True
--> x = True
--> x is not None
True
--> x = ('this','is','a','tuple')
--> x is not None
True
--> x = None
--> x is not None
False

~Ethan~



More information about the Python-list mailing list