Python: AttributeError: 'NoneType' object has no attribute

Python throws 'NoneType object has no attribute...' and points at a line that looks fine. The real cause is one step earlier: something you expected to hold a value is actually None. Here's how to find it.

BytExplorer 5 min read July 1, 2026

Python stops with something like:

AttributeError: 'NoneType' object has no attribute 'append'

The trick to this one: the problem usually isn't the line in the error. None is Python's "nothing here" value, and this error means you called a method or accessed an attribute on a variable that turned out to be None. So the real question is: why is that thing None? — and the answer is almost always a step or two earlier.

The usual causes

Three patterns cause the overwhelming majority:

  • A function with no return. It runs, does its work, but hands back None by default — and you stored that.
  • A method that changes things in place and returns None. list.sort(), list.append(), and dict.update() all mutate and return nothing. x = my_list.sort() makes x None.
  • A lookup that missed. dict.get('key') returns None when the key is absent; some library calls return None on "not found."

Step 1: Find what's actually None

Read the error's attribute name — it tells you what kind of thing you expected. Then print the variable right before the failing line:

result = find_user(id)
print(repr(result))     # is it None? then find_user returned nothing
result.name             # ← the AttributeError fires here

Step 2: Fix the source, not the symptom

If a function returns None when it shouldn't, add the return:

def double(x):
    x * 2          # bug: computes but returns nothing
def double(x):
    return x * 2   # fix

If you assigned the result of an in-place method, don't — call it, then use the original:

nums = [3, 1, 2]
nums.sort()        # sorts nums in place; don't capture the return
print(nums)        # [1, 2, 3]

"NoneType has no attribute X" is really "the thing I expected here is missing." Chase where it became None; the failing line is just where the emptiness got noticed.

Step 3: Guard when None is legitimate

If a value can legitimately be missing, check before you use it:

user = find_user(id)
if user is not None:
    print(user.name)

The checklist

  1. The error means a variable is None, not that the method is wrong.
  2. print(repr(x)) just before the failing line to confirm what's None.
  3. Trace back: missing return? captured an in-place method? a .get() miss?
  4. Fix the source; guard with if x is not None: where None is valid.
Put it into practice

Stop reading, start building

This pairs with a hands-on BytExplorer course — do it on your own machine and actually keep the skill.

More in Troubleshooting