Home › Errors › AttributeError
Python AttributeError: that object has no such attribute
Exception: AttributeErrorCategory: attribute lookupRaised at: runtime
What this error means
You asked an object for a name it does not have. The message is unusually generous — it states both halves of the mismatch: AttributeError: 'str' object has no attribute 'append' tells you the type you actually hold and the attribute you asked for.
So this is rarely a mystery about the attribute; it is almost always a wrong assumption about the type. Read the error as a sentence: *the value here is a str, and strings cannot append*. The real question becomes why is this value a string? — and the answer is usually a few lines above.
Python 3.10+ adds a spelling suggestion when it can: AttributeError: 'list' object has no attribute 'lenght'. Did you mean: 'length'?-style hints turn the typo case into a one-second fix.
A real example
names = "asha,ravi,dev"
names.append("priya")
print(names)
Traceback (most recent call last):
File "roster.py", line 2, in <module>
names.append("priya")
^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'append'
The fix is not to find a string version of append — it is to decide what names should be. If it is a collection, build it with names = "asha,ravi,dev".split(",") and append works. If it is genuinely text, use concatenation or an f-string instead.
The NoneType trap
'NoneType' object has no attribute '...' deserves its own heading, because it is the single most common form of this error — and the one whose cause is never on the line that crashes.
scores = [70, 95, 82]
top = scores.sort()
print(top.pop())
Traceback (most recent call last):
File "roster.py", line 3, in <module>
print(top.pop())
^^^^^^^
AttributeError: 'NoneType' object has no attribute 'pop'
list.sort() sorts in place and returns None. So do append, extend, insert, remove, reverse, clear and dict.update — the Python convention is that a method which mutates an object returns nothing. Assigning their result throws away your data and stores None.
- Mutating in place? Call it on its own line:
scores.sort(), then usescores. - Want a new object? Use the non-mutating twin:
sorted(scores),reversed(seq),list(a) + list(b). - A function that falls off the end without
returnalso hands backNone— a very common source of this error one call later. re.search(),dict.get()andnext(..., None)returnNonewhen there is no match; check the result before using it.
How to debug it
- Print the type right before the failing line:
print(type(value), value). The type in the error and the type you expected will disagree. - If the type is
NoneType, walk backwards to where the value was assigned — look for an in-place method, a function with noreturn, or a lookup that found nothing. - Check the spelling against
dir(value)—dirlists every attribute the object really has, and the near-miss usually jumps out (lenghtvslength,stripvstrim). - Confirm you are calling the right thing: a module (
import math, thenmath.sqrt), an instance method, or a class attribute — importingdatetimethe module and expecting thedatetimeclass is a classic. - Remember strings are immutable:
s.upper()returns a new string and changes nothing in place, so the result must be assigned.
Fix it interactively
Every problem below ends in an AttributeError until you repair the type assumption behind it:
Practice more Python bugs →Related errors
- TypeError — the same wrong-type assumption, caught at the operation instead of the attribute lookup.
- NameError — the name does not exist at all, rather than existing on the wrong object.
- KeyError — dictionaries use
d['key'], notd.key— mixing the two produces one of these two errors.
Every message in one place: the Python errors guide. To practise spotting the wrong type before you run the code, try output prediction.
🇮🇳 Hindi में समझें
AttributeError का मतलब — जिस object पर आपने method/attribute लगाया, उसके पास वो है ही नहीं। Message में दोनों चीज़ें लिखी होती हैं: object का type और माँगा गया attribute (जैसे 'str' object has no attribute 'append')। सबसे common case है 'NoneType' object has no attribute ... — क्योंकि sort(), append(), reverse() जैसे in-place methods None return करते हैं, उन्हें variable में assign मत करो।