comparison with None

Steve Holden steve at holdenweb.com
Thu Apr 19 14:18:30 CEST 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

to

   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.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list