{ }codelper

Error Messages › Python

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)
When you run it
Traceback (most recent call last):
  File "main.py", line 3, in <module>
AttributeError: 'NoneType' object has no attribute 'append'

Edit and run it →

Fixed code

Drop the a = and write just a.append(x).

a = []for x in [1, 2, 3]:    a.append(x)print(a)
Output
[1, 2, 3]

Edit and run it →

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)
When you run it
Traceback (most recent call last):
  File "main.py", line 3, in <module>
AttributeError: 'NoneType' object has no attribute 'reverse'

Edit and run it →

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.

a = [3, 1, 2]b = sorted(a)b.reverse()print(b)
Output
[3, 2, 1]

Edit and run it →

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())
When you run it
Traceback (most recent call last):
  File "main.py", line 5, in <module>
AttributeError: 'NoneType' object has no attribute 'upper'

Edit and run it →

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())
Output
HELLO

Edit and run it →

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())
When you run it
Traceback (most recent call last):
  File "main.py", line 3, in <module>
AttributeError: 'NoneType' object has no attribute 'upper'

Edit and run it →

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())
Output
unknown
UNKNOWN

Edit and run it →

What to check when you see this error

  1. On the line where the error happened (the number after line), find the name before the dot (.). That variable is None.
  2. Look upward for the line that put a value into that variable (name = ...). The cause is on that line.
  3. If the right side of = ends with append(), sort(), reverse() or print(), the variable gets None. Delete the name = part.
  4. If the right side of = is a function you wrote, check that every branch of it has a return. If it only returns inside an if, you get None when the condition is false.

Practice problems

See also