As pointed out by Aaron Hall's comment:
Since you can't subclass
NoneType
and sinceNone
is a singleton,isinstance
should not be used to detectNone
- instead you should do as the accepted answer says, and useis None
oris not None
.
Original Answer:
The simplest way however, without the extra line in addition to cardamom's answer is probably:
isinstance(x, type(None))
So how can I question a variable that is a NoneType? I need to use if method
Using isinstance()
does not require an is
within the if
-statement:
if isinstance(x, type(None)):
#do stuff
Additional information
You can also check for multiple types in one isinstance()
statement as mentioned in the documentation. Just write the types as a tuple.
isinstance(x, (type(None), bytes))