None in Python: the absence of a value

None stands for the absence of a value in Python: not zero, not empty, not False, but a single object tested with is.
5 min read
Believemy logo

Definition

A program constantly runs into moments where it has nothing to put in a variable: a search that turned up nothing, a form field left empty, a calculation not made yet. Zero would suggest a numeric result, an empty string would suggest missing text, yet neither is really the case: there is simply nothing there. Python answers that need with None, a reserved word that means precisely that.

Under the hood, None is a single object across the entire program: wherever it shows up, variable, argument or function result, it always points at the very same instance. That detail decides the right way to test for it, covered further down.

PYTHON
result = None

if result is None:
    print("Not computed yet")

It turns up in a few specific places. A function with no return hands it back automatically. A failed lookup often returns it, rather than crashing the program. And an optional argument left out at the call frequently defaults to None, a use covered in detail below.


What it is not

The most common mix-up files None alongside the other values Python treats as false in a test, right next to 0 or an empty list. A condition such as if not value: treats them the same way, but they do not tell the same story.

The table below lines them up side by side, one at a time:

ValueMeaning
NoneThere is no value
0There is a value, and it is zero
""There is a string, and it is empty
FalseThere is a boolean, and it is false
[]There is a list, and it is empty

This difference sounds purely theoretical until the day zero becomes a valid answer. A bank account showing 0 is not an account with an unknown balance, and a form field returning an empty string is not a field left untouched. Mixing the two does not break anything right away: the bug shows up later, on an edge case nobody planned for.


Test it with is, never with ==

How does one check that a variable holds None? The natural instinct is to write == None, as with any other comparison, but that is not the right question. Since only one None exists across the entire program, the question is not whether the value equals None, but whether it is None. The first asks about a value, using ==; the second asks about an identity, using is, and it is that second question which always gives the correct answer.

The distinction stays invisible most of the time, until the day an object redefines its comparison method: it can then answer "yes" to == None without actually being None, whereas identity cannot be faked.

PYTHON
# Correct
if value is None:
    ...

# Avoid
if value == None:
    ...
Good to know

PEP 8, the language's official style guide, explicitly recommends is None. Tools such as ruff flag it as a style error the moment an == None shows up in the code.


The default argument trap

A classic trap awaits anyone who picks an empty list as the default value: the intent looks reasonable enough, offer every call a ready-made list rather than forcing the caller to supply one.

PYTHON
# Trap: the same list for everyone
def add(item, basket=[]):
    basket.append(item)
    return basket

The problem comes down to an execution detail few expect: Python evaluates the default value once, when the function is defined, not on every call. The list created that day stays shared across every later call, never reset, and grows from one call to the next.

The fix: give None as the default value, then build the real list inside the function, only when it is missing.

PYTHON
# Fix: None as a sentinel
def add(item, basket=None):
    if basket is None:
        basket = []
    basket.append(item)
    return basket

This is the most widespread use of None outside function returns: a sentinel meaning "nothing was supplied", precisely because no legitimate value can accidentally look like it.

Warning

The trap is not limited to lists: an empty dictionary or an empty set create exactly the same problem as a default value. The same fix, None followed by creation inside the function, applies to all three.


Frequently asked questions

Question

Why does my function return None?

Because it holds no return, or because a particular execution path skips it. The same goes for methods that modify an object in place, such as sort: they work on the object handed to them and give nothing back.

Question

Is None the same as null in other languages?

The intent is the same across languages, but the implementation differs. None is a genuine object, with a type called NoneType, whereas null often denotes an empty pointer. That means None can be assigned, passed as an argument and compared without ever risking a crash.

Question

How can a missing key be told apart from a key holding None?

With in rather than a direct lookup: if "key" in my_dict answers about the key's presence, while reading its value with get returns None either way. This distinction turns critical as soon as json from an outside source is involved.

Related terms

Discover our python glossary

Browse the terms and definitions most commonly used in development with Python.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.