Null in Python: None Explained, and the Mistakes It Causes

Python has no null. It has None — and the difference is more than naming.

None is a singleton object of type NoneType. There is exactly one of it in the entire runtime, and every variable holding “nothing” points to that same object. That single fact explains most of the rules around it.

This guide covers what None is, how to test for it correctly, and the three mistakes it causes most often — including one that silently corrupts data across function calls.

The short version
Python’s null is calledNone
Its typeNoneType — and it’s a singleton
How to test itif x is None — never == None
The most common bugConfusing None with falsy values like 0 and ""
The worst bugUsing a mutable object as a default argument

None is a singleton

This is the fact everything else follows from.

Python creates exactly one None object when it starts, and every reference to “nothing” points to that same object:

a = None b = Noneprint(a is b) # True print(id(a) == id(b)) # True print(type(None)) # <class 'NoneType'>

Two variables set to None are not two copies of a value — they are two names for the same object.

⚠️ And None is immutable and cannot be reassigned. Writing None = 5 is a syntax error, unlike languages where null can be shadowed.

is None, never == None

Because None is a singleton, identity comparison is the correct test — and it’s faster, because is compares memory addresses rather than calling a method.

But the real reason is correctness. Any class can override __eq__ and return whatever it wants:

class Weird: def __eq__(self, other): return Truew = Weird()print(w == None) # True ← wrong answer print(w is None) # False ← correct answer

The object is not None, and is says so. The == operator asked the object, and the object lied.

⚠️ This isn’t hypothetical. Libraries like NumPy and pandas override __eq__ to return arrays rather than booleans — and array == None produces something that isn’t a boolean at all.

PEP 8 makes this explicit: comparisons to None should always use is or is not.

The most common bug: None is not the only falsy value

Comparison of five falsy values in Python showing that not x returns True for all of them while x is None returns True only for None

This is where most real bugs come from.

Python treats several values as false in a boolean context — and None is only one of them:

values = [None, 0, "", [], False]for v in values: print(repr(v), "| not v:", not v, "| is None:", v is None)# None | not v: True | is None: True # 0 | not v: True | is None: False # '' | not v: True | is None: False # [] | not v: True | is None: False # False | not v: True | is None: False

All five are falsy. Only one is None.

Why this breaks things

def get_quantity(value): return value or "not informed"print(get_quantity(None)) # not informed ← correct print(get_quantity(0)) # not informed ← WRONG print(get_quantity("")) # not informed ← WRONG

⚠️ A quantity of zero is a real value, and the function threw it away. The same happens with an empty string, an empty list, and False.

The fix is to test what you actually mean:

def get_quantity(value): if value is None: return "not informed" return valueprint(get_quantity(None)) # not informed print(get_quantity(0)) # 0 ← correct now

The rule: use if not x when you mean “empty or absent”. Use if x is None when you mean specifically absent.

The worst bug: mutable default arguments

This one is subtle, and it corrupts data across calls.

def add_item(item, items=[]): items.append(item) return itemsprint(add_item("a")) # ['a'] print(add_item("b")) # ['a', 'b'] ← the list was not empty

The second call should return ['b']. It returns ['a', 'b'].

The mutable default argument trap in Python: using an empty list as a default shares it across calls, while using None and creating the list inside the function works correctly

⚠️ The reason: default arguments are evaluated once, when the function is defined — not each time it’s called. There is a single list, shared by every call, and it grows forever.

The fix, and why None exists for it

def add_item(item, items=None): if items is None: items = [] items.append(item) return itemsprint(add_item("a")) # ['a'] print(add_item("b")) # ['b'] ← correct

None is immutable, so it’s safe as a default. The new list is created inside the function, on every call.

This pattern — =None in the signature, if x is None in the body — is the standard way to give a function a mutable default in Python. You’ll see it in every serious codebase.

Functions return None by default

A function with no return statement returns None — not nothing, not void:

def do_something(): print("working")result = do_something() print(result) # None print(result is None) # True

⚠️ And this catches people with methods that modify in place. list.append(), list.sort(), list.remove() and dict.update() all return None:

numbers = [1, 2, 3, None, 5]result = numbers.remove(None)print(result) # None ← the method returns None print(numbers) # [1, 2, 3, 5] ← the list was modified in place

The classic mistake is writing numbers = numbers.sort() — which assigns None to the variable and destroys the list.

The convention is deliberate: in Python, a method that modifies an object in place returns None, precisely so you don’t chain it by accident.

And there are more of these than most people realise — append, extend, insert, remove, sort, reverse, clear, update, add, discard and random.shuffle() all behave the same way. Our guide to every Python method that returns None has the full list, and why the bug is worse on a server than on your laptop.

When None raises an error

None has almost no attributes and no methods of its own. Any attempt to use it as an object fails:

value = Nonevalue.month # AttributeError: 'NoneType' object has no attribute 'month'value + 1 # TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'sorted([3, None, 1]) # TypeError: '<' not supported between instances of 'NoneType' and 'int'

⚠️ AttributeError: 'NoneType' object has no attribute... is one of the most common errors in Python — and it almost always means a function you called returned None when you expected an object.

The usual causes: a database query that found nothing, a regex that didn’t match, a dict.get() on a missing key, or a function that forgot its return.

Working with None safely

dict.get() with a fallback

data = {"name": None, "age": 25}print(data.get("name", "fallback")) # None ← the key exists print(data.get("missing", "fallback")) # fallback ← the key doesn't

⚠️ A subtlety worth knowing: the default of .get() applies when the key is missing, not when its value is None. A key that exists with value None returns None, not the fallback.

Chaining safely

user = find_user(email)if user is not None and user.address is not None: city = user.address.city else: city = "unknown"

Python has no safe navigation operator — no ?. like JavaScript or Kotlin. Explicit checks are the way.

Type hints: declaring that None is possible

Optional[X] means “either an X or None“:

from typing import Optionaldef find_user(user_id: int) -> Optional[str]: if user_id == 1: return "Ana" return None

Since Python 3.10, there’s a shorter form that doesn’t need the import:

def find_user(user_id: int) -> str | None: if user_id == 1: return "Ana" return None

Type hints don’t enforce anything at runtime — Python won’t stop you from returning something else. But tools like mypy and your editor will warn you when you use the result without checking for None first.

None compared with other languages

LanguageNull is calledHow it differs
PythonNoneA singleton object with its own type
JavaScriptnull and undefinedTwo separate concepts, which Python collapses into one
PHPnullCase-insensitive, and coerces loosely in comparisons
JavanullA reference, not an object — it has no type of its own
SQLNULLNULL = NULL is not true — it’s unknown

⚠️ The JavaScript distinction is the one that trips people up. There, null means “deliberately empty” and undefined means “never assigned” — Python has only None for both.

Run your Python where you control the version

The str | None syntax needs Python 3.10 — and plenty of shared hosts are still on 3.8. Copahost VPS plans give you root access, the Python version of your choice, and no restriction on what you install. From €3.99/month, with snapshots and support that reads the traceback with you.

See VPS plans

Frequently asked questions

Does Python have null?

Not by that name. Python’s equivalent is None, a singleton object of type NoneType. Writing null in Python raises a NameError — it isn’t a keyword and isn’t defined.

Why use is None instead of == None?

Because None is a singleton, so identity is the correct comparison — and because any class can override __eq__ to return True for anything. is compares object identity and cannot be overridden, so it always gives the right answer. PEP 8 requires it.

Is None the same as False?

No. None is falsy in a boolean context, but so are 0, "", [], {} and False itself. if not x is true for all of them; only if x is None distinguishes absence from emptiness.

What does “‘NoneType’ object has no attribute” mean?

That you called a method or accessed an attribute on None. It almost always means a function returned None when you expected an object — a query that found nothing, a regex that didn’t match, or a function missing its return.

Why does my list become None after sorting?

Because list.sort() sorts in place and returns None. Writing my_list = my_list.sort() assigns None to the variable and loses the list. Either call my_list.sort() on its own line, or use sorted(my_list), which returns a new list.

Why is a mutable default argument dangerous?

Because default arguments are evaluated once, when the function is defined — not on each call. A list used as a default is shared across every call and keeps growing. The fix is =None in the signature and if x is None: x = [] in the body.

What’s the difference between None and an empty string?

None means absent — there is no value. An empty string means present but empty — someone submitted a blank field. The distinction matters in forms and databases: “not answered” and “answered with nothing” are different facts.

Can I compare None with < or >?

No. None supports no ordering comparisons, and trying raises TypeError: '<' not supported between instances of 'NoneType' and 'int'. This is why sorting a list containing None fails — filter them out first, or pass a key function that handles them.

What is Optional in type hints?

Optional[X] declares that a value is either an X or None. Since Python 3.10 you can write X | None instead, without importing anything. Neither enforces anything at runtime — they exist so tools like mypy and your editor can warn you before the code runs.

Does dict.get() return the default when the value is None?

No, and this catches people. The default applies only when the key is missing. A key that exists with value None returns None, not your fallback — because from the dictionary’s perspective, it found what you asked for.

Is None the same as NULL in SQL?

Similar in intent, different in behaviour. In SQL, NULL = NULL is not true — it’s unknown, which is why you write IS NULL. In Python, None is None is simply True. Database drivers convert SQL NULL to Python None when reading rows.

How do I check for None across a chain of attributes?

With explicit checks — Python has no safe navigation operator like JavaScript’s ?.. Write if user is not None and user.address is not None, or wrap the access in a try/except AttributeError.

Conclusion

Python has no null. It has None — a single object, existing once, that every “nothing” in your program points to. That one fact explains every rule around it.

Three things carry the whole topic. Test with is None, never ==, because identity can’t be overridden and equality can. None is not the only falsy value — 0, "" and [] are too, and if not x treats them all the same. And never use a mutable object as a default argument; the =None pattern exists precisely to avoid that.

And when you see AttributeError: 'NoneType' object has no attribute..., the bug is rarely where the error appears. Something upstream returned None — a query with no rows, a regex with no match, a function missing its return. That’s where to look.

Share the Post:
Picture of Gustavo Gallas

Gustavo Gallas

Graduated in Computing at PUC-Rio, Brazil. Specialized in IT, networking, systems administration and human and organizational development​. Also have brewing skills.