comparison with None

Steve Holden steve at
Thu Apr 19 08:18:30 EDT 2007

Steven Howe wrote:
> Alan Isaac wrote: [type comparison stuff]
> I love scripting languages ... but sometimes an explicit evaluation that 
> one would find in
> a compiled language is better.

"better" in what sense?

> Which is why I suggested using the explicit type(x) == types.NoneType as 
> opposed to
> x is None
This seems to go entirely against the spirit of the language. It's about 
as sensible as writing

   (3 > 4) == True

The language *guarantees* that there is only one instance of 
types.NoneType, so why not just test for it directly? Among other things 
this avoids the need for an explicit import of the types library just so 
you can access the namespace.

For extra marks, please explain why you prefer

   type(x) == type(None) # or types.NoneType


   type(x) is type(None)

The canonical test is, as has already been explained,

   x is None

and to use anything else hinders the readability of your code.

Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd
Skype: holdenweb
Recent Ramblings

More information about the Python-list mailing list