is None or == None ?

Raymond Hettinger python at rcn.com
Fri Nov 6 12:21:55 EST 2009


On Nov 6, 5:20 am, mk <mrk... at gmail.com> wrote:
> Some claim that one should test for None using:
>
> if x is None:
>
> ..but the standard equality which is theoretically safer works as well:
>
> if x == None:
>
> So, which one is recommended?

In the standard library, we use "x is None".

The official recommendation in PEP 8 reads:
'''
Comparisons to singletons like None should always be done with
      'is' or 'is not', never the equality operators.

      Also, beware of writing "if x" when you really mean "if x is not
None"
      -- e.g. when testing whether a variable or argument that
defaults to
      None was set to some other value.  The other value might have a
type
      (such as a container) that could be false in a boolean context!
'''


Raymond



More information about the Python-list mailing list