← Back

Home › Errors › AttributeError

AttributeError: 'NoneType' object has no attribute — decoded

Exception: AttributeErrorCategory: runtimeRaised at: the moment you call a method on None

What this message means

NoneType is the type of exactly one value: None — Python's word for "nothing here". This error says you called a method or read an attribute on a variable that holds None, almost always because a function or method returned None when you expected it to return a value.

The attribute name in the message is a clue, not the crime. 'NoneType' object has no attribute 'append' does not mean append is wrong — it means the thing you called .append on is not a list at all, it is None.

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    result.append(4)
    ^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'append'

A real example

items = [3, 1, 2]

result = items.sort()    # sort() sorts IN PLACE and returns None
result.append(4)         # AttributeError

The bug is on the line *above* the traceback. list.sort() mutates the list and hands back None; storing its return value stores None. The fix is to use the list itself:

items = [3, 1, 2]

items.sort()             # the sorted list is items — always was
items.append(4)

result = sorted(items)   # and when you DO want a returned value: sorted(), not .sort()

The methods that return None (memorise this list)

The fix, step by step

  1. Read the variable name before the dot in the traceback — that name currently holds None.
  2. Find where it was last assigned. If the assignment is x = something.method(), check that method's return value in the docs — the in-place mutators above return None by design.
  3. If it is x = my_function(), open my_function and walk every branch: does each one end in a return with a value?
  4. Fix the producer (return something real, or drop the assignment) rather than papering over the consumer.
  5. For values that are legitimately sometimes None (a search that found nothing), check before using: if result is not None:.
def find_user(users, name):
    for user in users:
        if user["name"] == name:
            return user        # found
    return None                # not found — say so EXPLICITLY

user = find_user(users, "Aarav")
if user is not None:
    print(user["email"])
else:
    print("No such user")

Why Python is strict about None

A language that silently ignored .append on None would hide the fact that your data pipeline broke three lines earlier. Python makes the nothing loud: None has no attributes at all, so the first use after the break is where you find out. The AttributeError is doing you a favour — the real bug is always at least one line *above* the traceback.

Related messages worth knowing

Fix it interactively

These free problems practice exactly this — knowing what a function really returns:

Practice more Python bugs →
🇮🇳 Hindi में समझें

NoneType सिर्फ एक ही value का type है — None, यानी "यहाँ कुछ नहीं है"। यह error तब आता है जब None पर method call कर दो। सबसे बड़ा कारण: kuch list methods (sort(), append()...) list बदलते हैं पर None return करते हैं — उनका result store कर लेना ही bug है (x = items.sort() गलत, items.sort() और फिर items use करो सही)। दूसरा कारण: function में return लिखना भूल गए। Traceback से एक line ऊपर देखो — असली गलती वहीं है। और जो cheez जान-बूझकर None हो सकती है (जैसे search का result), उसे if result is not None: से check करो।

पूरी Hindi explanation पढ़ें →