AttributeError: 'int' object has no attribute 'X' (Python)

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
7 min

banner

# Table of Contents

  1. AttributeError: 'int' object has no attribute 'X' (Python)
  2. AttributeError: 'int' object has no attribute 'append'
  3. AttributeError: 'int' object has no attribute 'split'
  4. AttributeError: 'int' object has no attribute 'encode'

# AttributeError: 'int' object has no attribute 'X' (Python)

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.

attributeerror int object has no attribute

Here is an example of how the error occurs.

main.py
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, you need to track down where exactly you are setting the value to an integer in your code and correct the assignment.

# Convert the integer to a string before calling a method

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.

main.py
example = 123 result = str(example).startswith('12') print(result) # ๐Ÿ‘‰๏ธ True

convert integer to string before calling method

A common cause of the error is trying to call the str.isdigit() method on an integer.

main.py
my_str = '123' my_str = 123 # โ›”๏ธ AttributeError: 'int' object has no attribute 'isdigit' print(my_str.isdigit())

attributeerror int object has no attribute isdigit

To solve the error, convert the value to a string before calling the str.isdigit() method.

main.py
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.

# Reassigning a variable to an integer by mistake

Most commonly the error is caused by reassigning a variable to an integer somewhere in your code.

main.py
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.

main.py
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.

# Check if the object contains an attribute before accessing it

If you need to check whether an object contains an attribute, use the hasattr function.

main.py
example = 5 if hasattr(example, 'lower'): print(example.lower()) else: print('Attribute is not present on object') # ๐Ÿ‘‰๏ธ this runs

check if int contains attribute before accessing it

The hasattr() function takes the following 2 parameters:

NameDescription
objectThe object we want to test for the existence of the attribute
nameThe 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.

Using the 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.

# Check what attributes the object has

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.

main.py
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.

If you try to access any attribute that is not in this list, you would get the error.

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.

# Table of Contents

  1. AttributeError: 'int' object has no attribute 'append'
  2. AttributeError: 'int' object has no attribute 'split'
  3. AttributeError: 'int' object has no attribute 'encode'

# AttributeError: 'int' object has no attribute 'append'

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.

attributeerror int object has no attribute append

Here is an example of how the error occurs.

main.py
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, you need to track down where exactly you are setting the value to an integer in your code and correct the assignment.

To solve the error in the example, we would have to remove the reassignment or correct it.

main.py
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.

main.py
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.

We accessed the list item at index 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.

main.py
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.

main.py
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.

# Table of Contents

  1. AttributeError: 'int' object has no attribute 'split'
  2. AttributeError: 'int' object has no attribute 'encode'

# AttributeError: 'int' object has no attribute 'split'

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.

attributeerror int object has no attribute split

Here is an example of how the error occurs.

main.py
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, you need to track down where exactly you are setting the value to an integer in your code and correct the assignment.

To solve the error in the example, we would have to remove the reassignment or correct it.

main.py
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:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt 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().

main.py
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.

main.py
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.

# AttributeError: 'int' object has no attribute 'encode'

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.

attributeerror int object has no encode

Here is an example of how the error occurs.

main.py
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, you need to track down where exactly you are setting the value to an integer in your code and correct the assignment.

To solve the error in the example, we would have to remove the reassignment or correct it.

main.py
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().

main.py
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.

main.py
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev