The Python Oracle

not None test in Python

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC E Schuberts Piano Sonata D 784 in A

--

Chapters
00:00 Question
00:21 Accepted answer (Score 1276)
01:16 Answer 2 (Score 141)
01:56 Answer 3 (Score 38)
02:51 Answer 4 (Score 25)
03:09 Thank you

--

Full question
https://stackoverflow.com/questions/3965...

Accepted answer links:
[declaring keyword functions with default parameters]: http://effbot.org/zone/default-values.ht...
[Johnsyweb points out]: https://stackoverflow.com/questions/3965...
[PEP 8]: http://www.python.org/dev/peps/pep-0008/
[Zen of Python]: http://www.python.org/dev/peps/pep-0020/
[pseudocode]: http://en.wikipedia.org/wiki/Pseudocode

Answer 2 links:
[PEP 8]: http://www.python.org/dev/peps/pep-0008/

Answer 3 links:
[dis]: https://docs.python.org/3/library/dis.ht...
[is not]: https://docs.python.org/3/reference/expr...
[!=]: https://docs.python.org/3/reference/expr...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #nonetype

#avk47



ACCEPTED ANSWER

Score 1338


if val is not None:
    # ...

is the Pythonic idiom for testing that a variable is not set to None. This idiom has particular uses in the case of declaring keyword functions with default parameters. is tests identity in Python. Because there is one and only one instance of None present in a running Python script/program, is is the optimal test for this. As Johnsyweb points out, this is discussed in PEP 8 under "Programming Recommendations".

As for why this is preferred to

if not (val is None):
    # ...

this is simply part of the Zen of Python: "Readability counts." Good Python is often close to good pseudocode.




ANSWER 2

Score 149


From, Programming Recommendations, PEP 8:

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!

PEP 8 is essential reading for any Python programmer.




ANSWER 3

Score 39


The best bet with these types of questions is to see exactly what python does. The dis module is incredibly informative:

>>> import dis
>>> dis.dis("val != None")
  1           0 LOAD_NAME                0 (val)
              2 LOAD_CONST               0 (None)
              4 COMPARE_OP               3 (!=)
              6 RETURN_VALUE
>>> dis.dis("not (val is None)")
  1           0 LOAD_NAME                0 (val)
              2 LOAD_CONST               0 (None)
              4 COMPARE_OP               9 (is not)
              6 RETURN_VALUE
>>> dis.dis("val is not None")
  1           0 LOAD_NAME                0 (val)
              2 LOAD_CONST               0 (None)
              4 COMPARE_OP               9 (is not)
              6 RETURN_VALUE

Notice that the last two cases reduce to the same sequence of operations, Python reads not (val is None) and uses the is not operator. The first uses the != operator when comparing with None.

As pointed out by other answers, using != when comparing with None is a bad idea.




ANSWER 4

Score 25


Either of the latter two, since val could potentially be of a type that defines __eq__() to return true when passed None.