is None or == None ?
python at rcn.com
Fri Nov 6 18:21:55 CET 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
-- e.g. when testing whether a variable or argument that
None was set to some other value. The other value might have a
(such as a container) that could be false in a boolean context!
More information about the Python-list