Brainlag
  1. Library
  2. Debugging
  3. Python

AttributeErrorWhat is an AttributeError?

An AttributeError means you used a method or attribute that this kind of value does not have, often on None or with a name from another language.

Python5 min

What AttributeError means

Output
AttributeError: 'NoneType' object has no attribute 'append'

The part after the dot, like .append or .upper, is an attribute. Each type has its own set. The message names the type you actually had and the attribute it does not have.

The usual causes:

  • A None you did not expect. items = items.sort() sets items to None, because sort changes the list and returns nothing.
  • A method borrowed from another language. Lists have append, not push.
  • The wrong type. Calling .upper() on a number, or .append() on a string.

How to find it

Read the type in quotes first. If it says NoneType, look for the line that gave that name its value, usually an assignment from a method that returns nothing. Otherwise check the attribute's spelling and whether this type has it: dir(x) lists them all.

Run it: push is not a list method

Press Run and read the error from the bottom up.

main.py to run
queue = ["Ana", "Ben"]
queue.push("Cleo")
print(queue)

The sorted playlist that vanishes

This should sort the playlist, add a bonus track at the end, and print it. Fix it.

exercise_1.py to run
playlist = ["Static", "Intro", "Neon Rain"]
playlist = playlist.sort()
playlist.append("Bonus")
print(playlist)

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.