AttributeError: 'int' object has no attribute 'X'
1 · The lesson
readWhat 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
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':
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.
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.
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:
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:
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:
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:
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] = 0and later doescounts[word].append(...). Usecollections.Counterordefaultdict(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
0in another, for examplereturn 0when nothing was found instead ofreturn [].
Related errors
- AttributeError: 'str' object has no attribute 'X': the same mistake when the value is text.
- AttributeError: 'list' object has no attribute 'X': the same mistake when the value is a list.
- AttributeError: 'NoneType' object has no attribute 'X': when the value is
None, often from a function with noreturn. - TypeError: 'int' object is not subscriptable: using
[ ]on a number instead of a method. - TypeError: object of type 'int' has no len(): using
len()on a number.
See Also
- All Python errors: the full index, by type and by when it happens.
- Variables (tutorial)
- Numbers in depth
- Lists (tutorial)
- Working with Strings (tutorial)
Found this useful? Share it with a friend who’s learning Python.