"x == None" vs "x is None"

fernando junior fernandojr.ifcg at live.com
Tue Jan 19 17:20:14 EST 2016


> I have seen at several places "x == None" and "x is None" within
> if-statements.
> What is the difference? 
> Which term should I prefer and why?
> 
> 
> -- 
> Ullrich Horlacher              Server und Virtualisierung
> Rechenzentrum IZUS/TIK         E-Mail: horlacher at tik.uni-stuttgart.de
> Universitaet Stuttgart         Tel:    ++49-711-68565868
> Allmandring 30a                Fax:    ++49-711-682357
> 70550 Stuttgart (Germany)      WWW:    http://www.tik.uni-stuttgart.de/

In this case, the Style Guide for Python Code [1] recommends use "is or is not, never the equality operators" [2].

If you use the pep8 tool [3] to check your code, the error code that will be raised is E711 [4].

[1] https://www.python.org/dev/peps/pep-0008/
[2] https://www.python.org/dev/peps/pep-0008/#programming-recommendations
[3] http://pep8.readthedocs.org/en/latest/intro.html
[4] http://pep8.readthedocs.org/en/latest/intro.html#error-codes



More information about the Python-list mailing list