Python 'NoneType' object has no attribute
AttributeError: 'NoneType' object has no attribute 'append'
None is the value that means "no value". You get this error when you call something like .append() or .upper() on a variable you thought held a list or a string, but it actually holds None. The line to find is not the one where the error happened but the earlier line that put None into that variable.
Cause 1. You assigned the result of append() back
append() changes the list itself and returns nothing (None). So if you write a = a.append(x), a becomes None on the first pass of the loop, and the second pass raises the error.
Wrong code
a = []for x in [1, 2, 3]: a = a.append(x)print(a)Traceback (most recent call last): File "main.py", line 3, in <module> AttributeError: 'NoneType' object has no attribute 'append'
Fixed code
Drop the a = and write just a.append(x).
Cause 2. You stored the result of sort() in a variable
sort() and reverse() also change the list itself and return None. If you store the result in a variable, that variable is None.
Wrong code
a = [3, 1, 2]b = a.sort()b.reverse()print(b)Traceback (most recent call last): File "main.py", line 3, in <module> AttributeError: 'NoneType' object has no attribute 'reverse'
Fixed code
If you need a new list, use sorted(). If it is fine to change the original list, write a.sort() on a line by itself.
Cause 3. The function has no return
A function that does not hand back a value with return returns None. This happens when the function only computes something or only calls print().
Wrong code
def clean(s): s.strip().lower() word = clean(" Hello ")print(word.upper())Traceback (most recent call last): File "main.py", line 5, in <module> AttributeError: 'NoneType' object has no attribute 'upper'
Fixed code
Put return in front of the value you want to hand back.
def clean(s): return s.strip().lower() word = clean(" Hello ")print(word.upper())HELLO
Cause 4. get() returned None
A dictionary's get() returns None instead of raising an error when the key is missing. If you use a dot on that result right away, you get this error.
Wrong code
users = {"mina": "Seoul"}city = users.get("jun")print(city.upper())Traceback (most recent call last): File "main.py", line 3, in <module> AttributeError: 'NoneType' object has no attribute 'upper'
Fixed code
Check for None before you use it, or give get() a default value as its second argument.
users = {"mina": "Seoul"}city = users.get("jun")if city is None: print("unknown")else: print(city.upper())print(users.get("jun", "unknown").upper())unknown UNKNOWN
What to check when you see this error
- On the line where the error happened (the number after
line), find the name before the dot (.). That variable isNone. - Look upward for the line that put a value into that variable (
name = ...). The cause is on that line. - If the right side of
=ends withappend(),sort(),reverse()orprint(), the variable getsNone. Delete thename =part. - If the right side of
=is a function you wrote, check that every branch of it has areturn. If it only returns inside anif, you getNonewhen the condition is false.
Practice problems
See also
- Python 'int' object is not subscriptable
- Python TypeError: 'int' object is not iterable
- Python TypeError: unsupported operand type(s)
- Python KeyError: 'kiwi'
- Syntax: Python List append(), extend() and insert()
- Syntax: Python sort() vs sorted(): Sort a List
- Syntax: Python Dictionary (dict): Keys and Values
- Learn it step by step: Python Functions
- All Python error messages
- Online Python interpreter