How to "test" NoneType in python?
I have a method that sometimes returns a NoneType value. So how can I question a variable that is a NoneType? I need to use if method, for example
if not new: new = '#'
I know that is the wrong way and I hope you understand what I meant.
So how can I question a variable that is a NoneType?
is operator, like this
if variable is None:
Why this works?
is nottest for object identity:
x is yis true if and only if
yare the same object.
x is not yyields the inverse truth value.
Since there can be only one instance of
is would be the preferred way to check
Hear it from the horse's mouth
Quoting Python's Coding Style Guidelines - PEP-008 (jointly defined by Guido himself),
Comparisons to singletons like
Noneshould always be done with
is not, never the equality operators.