Last updated: Apr 8, 2024
Reading timeยท7 min
The Python "AttributeError: 'int' object has no attribute" occurs when we try to access an attribute that doesn't exist on an integer.
To solve the error, make sure the value is of the expected type before accessing the attribute.
Here is an example of how the error occurs.
example = 123 print(type(example)) # ๐๏ธ <class 'int'> # โ๏ธ AttributeError: 'int' object has no attribute 'startswith' result = example.startswith('12')
We tried to call the startswith()
method on an integer and got the error.
If you print()
the value you are accessing the attribute on, it will be an
integer.
To solve the error in the example, we would have to convert the integer to a
string
to be able to access the string-specific startswith()
method.
example = 123 result = str(example).startswith('12') print(result) # ๐๏ธ True
A common cause of the error is trying to call the str.isdigit()
method on an
integer.
my_str = '123' my_str = 123 # โ๏ธ AttributeError: 'int' object has no attribute 'isdigit' print(my_str.isdigit())
To solve the error, convert the value to a string before calling the
str.isdigit()
method.
my_num = '123' print(str(my_num).isdigit()) # ๐๏ธ True
The str.isdigit() method returns
True
if all characters in the string are digits and there is at least 1
character, otherwise, False
is returned.
Most commonly the error is caused by reassigning a variable to an integer somewhere in your code.
nums = [1, 2, 3] # ๐๏ธ reassignment of nums to an integer nums = 1 # โ๏ธ AttributeError: 'int' object has no attribute nums.append(4) print(nums)
We set the nums
variable to a list initially but later assigned an integer to
it.
To solve the error in this scenario, we have to remove the reassignment or correct it.
nums = [1, 2, 3] nums.append(4) print(nums) # ๐๏ธ [1, 2, 3, 4]
The nums
variable doesn't get set to an integer anywhere in our code, so the
error isn't raised.
If you need to check whether an object contains an attribute, use the hasattr
function.
example = 5 if hasattr(example, 'lower'): print(example.lower()) else: print('Attribute is not present on object') # ๐๏ธ this runs
The hasattr() function takes the following 2 parameters:
Name | Description |
---|---|
object | The object we want to test for the existence of the attribute |
name | The name of the attribute to check for in the object |
The hasattr()
function returns True
if the string is the name of one of the
object's attributes, otherwise False
is returned.
hasattr
function would handle the error if the attribute doesn't exist on the object, however, you still have to figure out where the variable gets assigned an integer value in your code.A good way to start debugging is to print(dir(your_object))
and see what
attributes the object has.
Here is an example of what printing the attributes of an integer
looks like.
example = 5 # [..., 'as_integer_ratio', 'bit_count', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes'...] print(dir(example))
If you pass a class to the dir() function, it returns a list of names of the class's attributes, and recursively of the attributes of its bases.
To solve the error, either convert the value to the correct type before accessing the attribute, or correct the type of the value you are assigning to the variable before accessing any attributes.
The Python "AttributeError: 'int' object has no attribute 'append'" occurs
when we call the append()
method on an integer.
To solve the error, make sure the value you are calling append
on is of type
list
.
Here is an example of how the error occurs.
my_list = ['a', 'b', 'c'] # ๐๏ธ reassign variable to an integer my_list = 10 print(type(my_list)) # ๐๏ธ <class 'int'> # โ๏ธ AttributeError: 'int' object has no attribute 'append' my_list.append('d')
We reassigned the my_list
variable to an integer and tried to call the
append()
method on the integer which caused the error.
If you print()
the value you are calling append()
on, it will be an integer.
To solve the error in the example, we would have to remove the reassignment or correct it.
my_list = ['a', 'b', 'c'] my_list.append('d') print(my_list) # ๐๏ธ ['a', 'b', 'c', 'd']
The append() method adds an item to the end of a list.
Calling the method on an integer causes the error.
Make sure you aren't trying to access the list at a specific index when calling
append
.
fav_numbers = [1, 2, 3] # โ๏ธ AttributeError: 'int' object has no attribute 'append' fav_numbers[0].append(4) print(fav_numbers)
The fav_numbers
variable stores a list of numbers.
0
(which is the integer 1
) and called the append()
method on the result which caused the error.To solve the error in this scenario, remove the index accessor and call the
append()
method on the list.
fav_numbers = [1, 2, 3] fav_numbers.append(4) print(fav_numbers) # ๐๏ธ [1, 2, 3, 4]
You might also be assigning the result of calling a function that returns an integer to a variable.
def get_list(): return 100 my_list = get_list() # โ๏ธ AttributeError: 'int' object has no attribute 'append' my_list.append('d')
The my_list
variable gets assigned to the result of calling the get_list
function.
The function returns an integer, so we aren't able to call append()
on it.
To solve the error, you have to track down where the specific variable gets assigned an integer instead of a list and correct the assignment.
The Python "AttributeError: 'int' object has no attribute 'split'" occurs when
we call the split()
method on an integer.
To solve the error, make sure the value you are calling split
on is of type
string
.
Here is an example of how the error occurs.
my_string = 'bobby,hadz' my_string = 100 print(type(my_string)) # <class 'int'> # โ๏ธ AttributeError: 'int' object has no attribute 'split' print(my_string.split(','))
We reassigned the my_string
variable to an integer and tried to call the
split()
method on the integer which caused the error.
If you print()
the value you are calling split()
on, it will be an integer.
To solve the error in the example, we would have to remove the reassignment or correct it.
my_string = 'bobby,hadz' print(my_string.split(',')) # ๐๏ธ ['bobby', 'hadz']
The str.split() method splits the string into a list of substrings using a delimiter.
The method takes the following 2 parameters:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done (optional) |
If you need to silence the error and can't remove the call to split()
you can
convert the integer to a string before calling split()
.
example = 123 print(str(example).split(',')) # ๐๏ธ ['123']
If the separator is not found in the string, a list containing only 1 element is returned.
You might also be assigning the result of calling a function that returns an integer to a variable.
def get_string(): return 100 my_string = get_string() # โ๏ธ AttributeError: 'int' object has no attribute 'split' print(my_string.split(','))
The my_string
variable gets assigned to the result of calling the get_string
function.
The function returns an integer, so we aren't able to call split()
on it.
To solve the error, you have to track down where the specific variable gets assigned an integer instead of a string and correct the assignment.
The Python "AttributeError: 'int' object has no attribute 'encode'" occurs
when we call the encode()
method on an integer. To solve the error, make sure
the value you are calling encode
on is of type string.
Here is an example of how the error occurs.
my_str = 'hello world' my_str = 123 # โ๏ธ AttributeError: 'int' object has no attribute 'encode' print(my_str.encode('utf-8'))
We reassigned the my_str
variable to an integer and tried to call the
encode()
method on the integer which caused the error.
If you print()
the value you are calling encode()
on, it will be an integer.
To solve the error in the example, we would have to remove the reassignment or correct it.
my_str = 'hello world' print(my_str.encode('utf-8')) # ๐๏ธ b'hello world'
If you are trying to get the encoded version of a number as a bytes object,
convert the integer to a string before calling encode()
.
my_num = 1234 result = str(my_num).encode('utf-8') print(result) # ๐๏ธ b'1234'
The str.encode method returns an
encoded version of the string as a bytes object. The default encoding is
utf-8
.
You might also be assigning the result of calling a function that returns an integer to a variable.
def get_string(): return 100 my_string = get_string() # โ๏ธ AttributeError: 'int' object has no attribute 'encode' print(my_string.encode('utf-8'))
The my_string
variable gets assigned to the result of calling the get_string
function.
The function returns an integer, so we aren't able to call encode()
on it.
To solve the error, you have to track down where the specific variable gets assigned an integer instead of a string and correct the assignment.