PythonMastery
reference 3 min read · lesson 68 of 80 in Errors

AttributeError: 'int' object has no attribute 'X'

1 · The lesson

read

What this error means

You asked a whole number for a method it doesn't have. Numbers have very few methods, and none of the list or string ones. The name at the end of the message tells you what you thought you were holding: append means you expected a list, isdigit or split means you expected a string, length or size means you expected something with a length. The variable holds an int. The real bug is usually in the line where it got that value.

When you see it

python
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    count.append(1)
    ^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'append'

A decimal number gives the same message with 'float':

python
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    x.append(1)
    ^^^^^^^^
AttributeError: 'float' object has no attribute 'append'

Why it happens

1. A number where you wanted a list. A variable started as 0 to count something, and later code treats it as the list of things.

python
count = 0
count.append(1)               # AttributeError: no attribute 'append'

2. A string method on a number. .isdigit(), .split() and .zfill() belong to strings. If the value was already converted with int(), or came from JSON or a database as a number, those methods are gone.

python
n = 42
print(n.isdigit())            # AttributeError: no attribute 'isdigit'

3. Habits from other languages. JavaScript has .length, and some libraries use .size. Python uses the built-in function len(), and a number has no length at all.

4. A variable reused for two jobs. total held a list of prices, and then total = sum(total) replaced it with a number. Later code still expects the list.

How to fix it

Keep the count and the list in separate variables:

python
items = []
count = 0
for word in ["red", "green", "blue"]:
    items.append(word)
    count += 1
print(items, count)           # ['red', 'green', 'blue'] 3

You often don't need the counter: len(items) gives the same number.

Use string methods only on strings. If you need to check text before converting, check it first, then convert:

python
text = "42"
if text.isdigit():
    n = int(text)
    print(n + 1)              # 43

If you only need the number as text, convert it back with str(): str(7).zfill(3) gives '007'.

Use len() for the length, and only on a list, string or other collection:

python
names = ["Ada", "Grace"]
print(len(names))             # 2

To count the digits of a number, turn it into text first: len(str(12345)) gives 5.

Give each value its own name so a later line can't replace the list with a number:

python
prices = [4.5, 2.0, 3.5]
total = sum(prices)
print(prices, total)          # [4.5, 2.0, 3.5] 10.0

When you're not sure, print type(x) on the line before the error. The first line where it shows <class 'int'> instead of what you expected is where to look.

When you'd see this in real code

  • A word counter that starts with counts[word] = 0 and later does counts[word].append(...). Use collections.Counter or defaultdict(list), depending on what you want to store.
  • JSON from an API, where a field you expected as text, like an ID or a ZIP code, is a number: data["id"].startswith("A") fails.
  • pandas or numpy values, which give messages like 'numpy.int64' object has no attribute 'append' for the same reason.
  • A function that returns a list in one branch and 0 in another, for example return 0 when nothing was found instead of return [].

See Also

Found this useful? Share it with a friend who’s learning Python.