The Proper Way to Check for None in Python

Posted by Afsal on 15-Nov-2024

Hi Pythonistas!

When working with Python, you’ll often find yourself needing to check whether a variable is None. But did you know there’s a specific way to do this that’s considered both correct and efficient? Let's dive into the right way to check for None in Python, and why common shortcuts can sometimes lead to unexpected results.

Why Check for None?

In Python, None is a special singleton object used to denote “no value” or “null.” It’s often used as a default value for function parameters, or as a placeholder when a value isn’t yet assigned. Since None is distinct from 0, False, and an empty string (''), it’s essential to check for None specifically when you need to confirm that a variable genuinely lacks a value.

The Right Way: Using is or is not

The recommended way to check if a variable is None is with the is or is not operators. Here’s why:

None is a singleton: There is only one instance of None in a Python program. So, the is operator (which checks identity) is the perfect tool to check if a variable is None.

Performance and readability: is is slightly faster than == and is more explicit in intent. Using is None clearly shows that you’re checking for the presence of the None object.

Example:

my_variable = None

if my_variable is None:
    print("Variable is None")
else:
    print("Variable is not None")

The code above will print “Variable is None” if my_variable indeed holds a None value.

What About if my_variable:?

It’s common to see if my_variable: used as a shortcut to check if a variable is “truthy.” But this is not the same as checking for None.

Here’s how it works:

Truthy values in Python are any values that evaluate to True in a boolean context, like non-zero numbers, non-empty strings, lists, and dictionaries.

Falsy values are values that evaluate to False in a boolean context. These include None, 0, False, '' (empty string), [] (empty list), and {} (empty dictionary).

So, when you use if my_variable:, you’re not only checking if my_variable is None, but also if it’s any other falsy value. This can lead to unexpected behavior.

Example of Potential Confusion:

my_variable = 0

if my_variable:
    print("Variable is truthy")
else:
    print("Variable is falsy")  # This will print because 0 is falsy.

In this example, my_variable is 0, which is falsy, so the code incorrectly considers it as “not truthy” even though it’s not None.

When Should You Use if my_variable:?

Use if my_variable: when you want to check if a variable has any truthy value—not when you specifically want to check for None. This shorthand works well when you're looking for general “emptiness” or “presence,” not a None check.

For example:

name = "John"

if name:
    print("Name is set")
else:
    print("Name is empty or None")

 

In this case, if name: will evaluate as True for any non-empty string. But if name is None, '', or any other empty value, it will print “Name is empty or None.”

The Key Takeaway

If you need to check if a variable is specifically None, use is None or is not None. This approach ensures that only the None object is targeted, leading to more readable and predictable code.

Correct way to check for None

if my_variable is None:
    print("Variable is None")

Correct way to check if not None

if my_variable is not None:
    print("Variable has a value that is not None")

By following this simple guideline, you’ll avoid unexpected bugs and write Pythonic code that’s easy to read and maintain.