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)
list.sort(),list.reverse()— in-place mutators. The *list* changes; the return isNone. Their value-returning twins aresorted()andreversed().list.append(),extend(),insert(),remove(),clear(),update()on dicts,add()on sets — all mutate in place, all returnNone.print()— returnsNone.text = print("hi")storesNone.- A function with no
return— falls off the end and returnsNone. So does a function whosereturnis bare:return. - A function that only returns on some paths — the branch you took had no
returnat all.
The fix, step by step
- Read the variable name before the dot in the traceback — that name currently holds
None. - 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 returnNoneby design. - If it is
x = my_function(), openmy_functionand walk every branch: does each one end in areturnwith a value? - Fix the producer (return something real, or drop the assignment) rather than papering over the consumer.
- 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
- AttributeError: the parent guide — reading the type in the message, and every famous wording decoded.
- TypeError: int is not subscriptable — the same producer-lied-to-consumer shape, with an int instead of None.
- AttributeError (Hindi) — the full explanation in Hindi, with browser practice.
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 करो।