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.

Asked By: CrveniZg



So how can I question a variable that is a NoneType?

Use is operator, like this

if variable is None:

Why this works?

Since None is the sole singleton object of NoneType in Python, we can use is operator to check if a variable has None in it or not.

Quoting from is docs,

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.

Since there can be only one instance of None, is would be the preferred way to check None.

Hear it from the horse’s mouth

Quoting Python’s Coding Style Guidelines – PEP-008 (jointly defined by Guido himself),

Comparisons to singletons like None should always be done with is or is not, never the equality operators.

Answered By: thefourtheye
if variable is None:

if variable is not None:
Answered By: elhoucine

It can also be done with isinstance as per Alex Hall’s answer :

>>> NoneType = type(None)
>>> x = None
>>> type(x) == NoneType
>>> isinstance(x, NoneType)

isinstance is also intuitive but there is the complication that it requires the line

NoneType = type(None)

which isn’t needed for types like int and float.

Answered By: cardamom

As pointed out by Aaron Hall’s comment:

Since you can’t subclass NoneType and since None is a singleton, isinstance should not be used to detect None – instead you should do as the accepted answer says, and use is None or is 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))
Answered By: Martin Müsli

Python 2.7 :

x = None
isinstance(x, type(None))


isinstance(None, type(None))

==> True

Answered By: Pesko

Not sure if this answers the question. But I know this took me a while to figure out. I was looping through a website and all of sudden the name of the authors weren’t there anymore. So needed a check statement.

if type(author) == type(None):
     print("my if body")
     print(" my else body")

Author can be any variable in this case, and None can be any type that you are checking for.

Answered By: Joshua Pachner

I hope this example will be helpful for you)

print(type(None))  # NoneType

So, you can check type of the variable name

# Example
name = 12  # name = None

if type(name) is type(None):
    print("Can't find name")
Answered By: alexmosk25

I mostly use the following method to check for NoneType.

if (new): # not None
else: # NoneType
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.