Post

A Quick Note on Python's None

In Python, None is a special constant that represents the absence of a value or a null value. It is an object of its own class, NoneType. It is not the same as 0, False, or an empty string. None is a singleton, meaning there is only one instance of it in memory.

What is None?

None is often used to signify that a variable has not yet been assigned a value, or that a function did not return anything.

1
2
3
4
my_variable = None

print(my_variable)      # None
print(type(my_variable)) # <class 'NoneType'>

Checking for None

The correct way to check if a variable is None is to use the is operator, not the == operator. This is because None is a singleton, so you are checking if the variable is the exact same object as None.

1
2
3
4
5
6
my_variable = None

if my_variable is None:
    print("The variable is None")
else:
    print("The variable is not None")

None as a Default Value

None is often used as a default value for function arguments. This allows you to have optional arguments that have a special meaning when they are not provided.

1
2
3
4
5
6
7
8
def my_function(arg1, arg2=None):
    if arg2 is None:
        print("arg2 was not provided")
    else:
        print(f"arg2 is {arg2}")

my_function("hello")
my_function("hello", "world")

Conclusion

None is a simple but important concept in Python. It provides a clear and consistent way to represent the absence of a value. By understanding how to use None correctly, you can write more readable, predictable, and Pythonic code.

This post is licensed under CC BY 4.0 by the author.