Checking whether type is None

Marko Rauhamaa marko at pacujo.net
Thu Jul 26 03:48:43 EDT 2018


Steven D'Aprano <steve+comp.lang.python at pearwood.info>:
> On Wed, 25 Jul 2018 16:14:18 +0000, Schachner, Joseph wrote:
>> thing is None looks just as odd to me. Why not thing == None ? That
>> works.
>
> It is wrong (in other words, it doesn't work) because it allows
> non-None objects to masquerade as None and pretend to be what they are
> not.
>
> If that's your intent, then of course you may do so. But without a
> comment explaining your intent, don't be surprised if more experienced
> Python programmers correct your "mistake".

Also, while

   thing == None

would work perfectly in almost all cases in practice, it's unidiomatic
and suggests the writer isn't quite comfortable with the workings of the
language.

Somewhat similarly, this code works perfectly:

   while (x > 0):
       y = y * x
       x = x - 1
   # end of while
   return(y)

but it doesn't look like Python.


Marko



More information about the Python-list mailing list