In Python, the 'null' object is the singleton None.

To check if something is None, use the is identity operator:

if foo is None:
    ...
Answer from Ben James on Stack Overflow
Top answer
1 of 11
1992

In Python, the 'null' object is the singleton None.

To check if something is None, use the is identity operator:

if foo is None:
    ...
2 of 11
266

None, Python's null?

There's no null in Python; instead there's None. As stated already, the most accurate way to test that something has been given None as a value is to use the is identity operator, which tests that two variables refer to the same object.

>>> foo is None
True
>>> foo = 'bar'
>>> foo is None
False

The basics

There is and can only be one None

None is the sole instance of the class NoneType and any further attempts at instantiating that class will return the same object, which makes None a singleton. Newcomers to Python often see error messages that mention NoneType and wonder what it is. It's my personal opinion that these messages could simply just mention None by name because, as we'll see shortly, None leaves little room to ambiguity. So if you see some TypeError message that mentions that NoneType can't do this or can't do that, just know that it's simply the one None that was being used in a way that it can't.

Also, None is a built-in constant. As soon as you start Python, it's available to use from everywhere, whether in module, class, or function. NoneType by contrast is not, you'd need to get a reference to it first by querying None for its class.

>>> NoneType
NameError: name 'NoneType' is not defined
>>> type(None)
NoneType

You can check None's uniqueness with Python's identity function id(). It returns the unique number assigned to an object, each object has one. If the id of two variables is the same, then they point in fact to the same object.

>>> NoneType = type(None)
>>> id(None)
10748000
>>> my_none = NoneType()
>>> id(my_none)
10748000
>>> another_none = NoneType()
>>> id(another_none)
10748000
>>> def function_that_does_nothing(): pass
>>> return_value = function_that_does_nothing()
>>> id(return_value)
10748000

None cannot be overwritten

In much older versions of Python (before 2.4) it was possible to reassign None, but not any more. Not even as a class attribute or in the confines of a function.

# In Python 2.7
>>> class SomeClass(object):
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: cannot assign to None
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to None

# In Python 3.5
>>> class SomeClass:
...     def my_fnc(self):
...             self.None = 'foo'
SyntaxError: invalid syntax
>>> def my_fnc():
        None = 'foo'
SyntaxError: cannot assign to keyword

It's therefore safe to assume that all None references are the same. There isn't any "custom" None.

To test for None use the is operator

When writing code you might be tempted to test for Noneness like this:

if value==None:
    pass

Or to test for falsehood like this

if not value:
    pass

You need to understand the implications and why it's often a good idea to be explicit.

Case 1: testing if a value is None

Why do

value is None

rather than

value==None

?

The first is equivalent to:

id(value)==id(None)

Whereas the expression value==None is in fact applied like this

value.__eq__(None)

If the value really is None then you'll get what you expected.

>>> nothing = function_that_does_nothing()
>>> nothing.__eq__(None)
True

In most common cases the outcome will be the same, but the __eq__() method opens a door that voids any guarantee of accuracy, since it can be overridden in a class to provide special behavior.

Consider this class.

>>> class Empty(object):
...     def __eq__(self, other):
...         return not other

So you try it on None and it works

>>> empty = Empty()
>>> empty==None
True

But then it also works on the empty string

>>> empty==''
True

And yet

>>> ''==None
False
>>> empty is None
False

Case 2: Using None as a boolean

The following two tests

if value:
    # Do something

if not value:
    # Do something

are in fact evaluated as

if bool(value):
    # Do something

if not bool(value):
    # Do something

None is a "falsey", meaning that if cast to a boolean it will return False and if applied the not operator it will return True. Note however that it's not a property unique to None. In addition to False itself, the property is shared by empty lists, tuples, sets, dicts, strings, as well as 0, and all objects from classes that implement the __bool__() magic method to return False.

>>> bool(None)
False
>>> not None
True

>>> bool([])
False
>>> not []
True

>>> class MyFalsey(object):
...     def __bool__(self):
...         return False
>>> f = MyFalsey()
>>> bool(f)
False
>>> not f
True

So when testing for variables in the following way, be extra aware of what you're including or excluding from the test:

def some_function(value=None):
    if not value:
        value = init_value()

In the above, did you mean to call init_value() when the value is set specifically to None, or did you mean that a value set to 0, or the empty string, or an empty list should also trigger the initialization? Like I said, be mindful. As it's often the case, in Python explicit is better than implicit.

None in practice

None used as a signal value

None has a special status in Python. It's a favorite baseline value because many algorithms treat it as an exceptional value. In such scenarios it can be used as a flag to signal that a condition requires some special handling (such as the setting of a default value).

You can assign None to the keyword arguments of a function and then explicitly test for it.

def my_function(value, param=None):
    if param is None:
        # Do something outrageous!

You can return it as the default when trying to get to an object's attribute and then explicitly test for it before doing something special.

value = getattr(some_obj, 'some_attribute', None)
if value is None:
    # do something spectacular!

By default a dictionary's get() method returns None when trying to access a non-existing key:

>>> some_dict = {}
>>> value = some_dict.get('foo')
>>> value is None
True

If you were to try to access it by using the subscript notation a KeyError would be raised

>>> value = some_dict['foo']
KeyError: 'foo'

Likewise if you attempt to pop a non-existing item

>>> value = some_dict.pop('foo')
KeyError: 'foo'

which you can suppress with a default value that is usually set to None

value = some_dict.pop('foo', None)
if value is None:
    # Booom!

None used as both a flag and valid value

The above described uses of None apply when it is not considered a valid value, but more like a signal to do something special. There are situations however where it sometimes matters to know where None came from because even though it's used as a signal it could also be part of the data.

When you query an object for its attribute with getattr(some_obj, 'attribute_name', None) getting back None doesn't tell you if the attribute you were trying to access was set to None or if it was altogether absent from the object. The same situation when accessing a key from a dictionary, like some_dict.get('some_key'), you don't know if some_dict['some_key'] is missing or if it's just set to None. If you need that information, the usual way to handle this is to directly attempt accessing the attribute or key from within a try/except construct:

try:
    # Equivalent to getattr() without specifying a default
    # value = getattr(some_obj, 'some_attribute')
    value = some_obj.some_attribute
    # Now you handle `None` the data here
    if value is None:
        # Do something here because the attribute was set to None
except AttributeError:
    # We're now handling the exceptional situation from here.
    # We could assign None as a default value if required.
    value = None
    # In addition, since we now know that some_obj doesn't have the
    # attribute 'some_attribute' we could do something about that.
    log_something(some_obj)

Similarly with dict:

try:
    value = some_dict['some_key']
    if value is None:
        # Do something here because 'some_key' is set to None
except KeyError:
    # Set a default
    value = None
    # And do something because 'some_key' was missing
    # from the dict.
    log_something(some_dict)

The above two examples show how to handle object and dictionary cases. What about functions? The same thing, but we use the double asterisks keyword argument to that end:

def my_function(**kwargs):
    try:
        value = kwargs['some_key']
        if value is None:
            # Do something because 'some_key' is explicitly
            # set to None
    except KeyError:
        # We assign the default
        value = None
        # And since it's not coming from the caller.
        log_something('did not receive "some_key"')

None used only as a valid value

If you find that your code is littered with the above try/except pattern simply to differentiate between None flags and None data, then just use another test value. There's a pattern where a value that falls outside the set of valid values is inserted as part of the data in a data structure and is used to control and test special conditions (e.g. boundaries, state, etc.). Such a value is called a sentinel and it can be used the way None is used as a signal. It's trivial to create a sentinel in Python.

undefined = object()

The undefined object above is unique and doesn't do much of anything that might be of interest to a program, it's thus an excellent replacement for None as a flag. Some caveats apply, more about that after the code.

With function

def my_function(value, param1=undefined, param2=undefined):
    if param1 is undefined:
        # We know nothing was passed to it, not even None
        log_something('param1 was missing')
        param1 = None


    if param2 is undefined:
        # We got nothing here either
        log_something('param2 was missing')
        param2 = None

With dict

value = some_dict.get('some_key', undefined)
if value is None:
    log_something("'some_key' was set to None")

if value is undefined:
    # We know that the dict didn't have 'some_key'
    log_something("'some_key' was not set at all")
    value = None

With an object

value = getattr(obj, 'some_attribute', undefined)
if value is None:
    log_something("'obj.some_attribute' was set to None")
if value is undefined:
    # We know that there's no obj.some_attribute
    log_something("no 'some_attribute' set on obj")
    value = None

As I mentioned earlier, custom sentinels come with some caveats. First, they're not keywords like None, so Python doesn't protect them. You can overwrite your undefined above at any time, anywhere in the module it's defined, so be careful how you expose and use them. Next, the instance returned by object() is not a singleton. If you make that call 10 times you get 10 different objects. Finally, usage of a sentinel is highly idiosyncratic. A sentinel is specific to the library it's used in and as such its scope should generally be limited to the library's internals. It shouldn't "leak" out. External code should only become aware of it, if their purpose is to extend or supplement the library's API.

🌐
Real Python
realpython.com › null-in-python
Null in Python: Understanding Python's NoneType Object – Real Python
December 15, 2021 - All variables in Python come into existence by assignment. A variable will only start life as null in Python if you assign None to it. ... Very often, you’ll use None as the default value for an optional parameter.
🌐
Copahost
copahost.com › home › null python: the complete guide to null values
Null Python: The Complete Guide to Null Values - Copahost
August 11, 2023 - In Python, we recommend using “None” instead of “null” to indicate the absence of a value. The native value used to indicate. The absence of a value is “None”, while “null” is a string that must be converted to a native value ...
🌐
NxtWave
ccbp.in › blog › articles › null-in-python
Null in Python: Understanding and Handling Null Values
None clearly distinguishes between ... when dealing with uninitialized variables or optional data. Python uses the keyword None to define null objects and variables....
🌐
FavTutor
favtutor.com › blogs › null-python
Null in Python: How to set None in Python? (with code)
Python doesn't have an attribute with the term null, to speak specifically. Python uses None in place of null. It's used to specify a null value or absolutely no value.
🌐
GeeksforGeeks
geeksforgeeks.org › python › null-in-python
Null in Python - GeeksforGeeks
July 23, 2025 - It's often used as a placeholder for variables that don't hold meaningful data yet. Unlike 0, "" or [], which are actual values, None specifically means "no value" or "nothing." Example: ... Explanation: This code checks if the variable a holds ...
🌐
LearnPython.com
learnpython.com › blog › null-in-python
Null in Python: A Complete Guide | LearnPython.com
The short answer is that there is no Null in Python, but there is the None object that Python programmers use to define null objects and variables.
🌐
AbsentData
absentdata.com › home › articles › handling nulls in python: a hands-on tutorial
Handling Nulls in Python: A Hands-On Tutorial - AbsentData
October 18, 2023 - We can simply declare null in Python using the data type None as follows. ... We can also check the data type of the variable using a pre-defined function type(). ... Output: Here we can see that the data type x_variable is None.
Find elsewhere
🌐
Enterprise DNA
blog.enterprisedna.co › python-null
Null in Python: 7 Use Cases With Code Examples – Master Data Skills + AI
This means you can use the id() function to compare variables to None using their identities: ... The getattr() function is another built-in Python function that allows you to access an object’s attributes by name. Given an object and a string, getattr() returns the value of the named attribute or the None object if the attribute doesn’t exist. This can be helpful for you when handling null values, especially in cases where you need to access attributes that might be missing.
🌐
Sentry
sentry.io › sentry answers › python › use the null object in python
Use the null object in Python | Sentry
None is Python’s equivalent of null. It is a singleton object of the class NoneType. It is universally available and cannot be reassigned. To test whether a variable is None, we should use Python’s is identity operator, as below: ... Using is is preferable to using ==, as the latter can be overloaded, which may lead to unexpected behavior when used with certain objects. None is falsy, which means that it will be considered equivalent to False when used in boolean expressions.
🌐
STechies
stechies.com › python-null
Null Object in Python with Example
# Python program to check None value # Using a custom function # initialized a variable with None value myval = None # Define function def nullfunction(myval): # Check if variable is None if myval is None: result = None else: result = 'myval is not None' return result print('Output: ',nullfunction(myval))
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › null object in python
Null Object in Python - Spark By {Examples}
May 31, 2024 - As mentioned above the keyword “None” allows you to create a null object, providing a means for initialization to the null reference. When a variable has not been assigned a valid value, you can use the keyword None to indicate that this ...
🌐
CodingNomads
codingnomads.com › python-null-value
Python Null Value
You can record that the data is missing by pointing it to Python's pit of nothingness, None · By using the None value, you clarify that this data point is missing. It was never recorded, or maybe it got lost somewhere on the way. All that you know is that it's not there. You might see that, in the context of this example, you can't use 0 to note the absence of a value.
🌐
AskPython
askpython.com › home › python null – how to identify null values in python?
Python NULL - How to identify null values in Python? - AskPython
April 10, 2023 - In simpler words, the None keyword is used to define a null variable or null object. def func_no_return(): a = 5 b = 7 print(func_no_return()) ... NOTE: Whenever we assign None to a variable, all the variables that are assigned to it point to ...
🌐
Real Python
realpython.com › courses › python-none
Python's None: Null in Python – Real Python
July 23, 2020 - Python uses the keyword None to define null objects and variables. While None does serve some of the same purposes as null in other languages, it’s another beast entirely. As the null in Python, None is not defined to be 0 or any other value.
🌐
Python Central
pythoncentral.io › python-null-equivalent-none
Python's null equivalent: None | Python Central
September 30, 2023 - What's more, 'NoneType' is immutable, just like the strings in Python. This means once the None object is created, it cannot be modified. And if you haven't noticed it yet, note that you cannot perform operations directly on None. There are two ways to check if a variable is None. One way can be performed by using the is keyword. Another is using the == syntax. Both comparison methods are different, and you'll see why later: [python] null_variable = None not_null_variable = 'Hello There!'
🌐
Python Pool
pythonpool.com › home › blog › python null | what is null in python | none in python
Python Null | What is Null in Python | None in Python - Python Pool
December 30, 2023 - In Python, there is no null keyword or object available. Instead, you may use the ‘None’ keyword, which is an object. We can assign None to any variable, but you can not create other NoneType objects.
🌐
Quora
quora.com › How-do-I-declare-a-null-value-in-Python
How to declare a null value in Python - Quora
Answer (1 of 6): The concept of “nothing” is a difficult thing to implement in a computer language where everything is “something” and has some value. Different language approach this differently. Hardware languages cannot have nothing, so they designate some value as a null value or create a “n...
🌐
TutorialsPoint
tutorialspoint.com › the-null-object-in-python
The Null Object in Python
A variable will be null in Python if you assign None to it. var_a = None print('var_a is: ',var_a) print(var_b) Running the above code gives us the following result − · Traceback (most recent call last): File "C:\Users\Pradeep\AppData\Roaming\JetBrains\PyCharmCE2020.3\scratches\scratch.py", ...