VOOZH about

URL: https://www.geeksforgeeks.org/python/python-attributeerror/

⇱ Python: AttributeError - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python: AttributeError

Last Updated : 12 Jul, 2025

In every programming language, if we develop new programs, there is a high chance of getting errors or exceptions. These errors yield to the program not being executed. One of the error in Python mostly occurs is "AttributeError". AttributeError can be defined as an error that is raised when an attribute reference or assignment fails. 
For example, if we take a variable x we are assigned a value of 10. In this process suppose we want to append another value to that variable. It's not possible. Because the variable is an integer type it does not support the append method. So in this type of problem, we get an error called "AttributeError". Suppose if the variable is list type then it supports the append method. Then there is no problem and not getting"Attribute error".

Note: Attribute errors in Python are generally raised when an invalid attribute reference is made.
There are a few chances of getting AttributeError.
Example 1:

Output:

Traceback (most recent call last):
 File "/home/46576cfdd7cb1db75480a8653e2115cc.py", line 5, in 
 X.append(6)
AttributeError: 'int' object has no attribute 'append'


Example 2: Sometimes any variation in spelling will cause an Attribute error as Python is a case-sensitive language.

Output:

Traceback (most recent call last):
 File "/home/2078367df38257e2ec3aead22841c153.py", line 3, in 
 string = "The famous website is { }".fst("geeksforgeeks")
AttributeError: 'str' object has no attribute 'fst'


Example 3: AttributeError can also be raised for a user-defined class when the user tries to make an invalid attribute reference.

Output: 

GeeksforGeeks

Error:

Traceback (most recent call last):
 File "/home/373989a62f52a8b91cb2d3300f411083.py", line 17, in 
 print(obj.b)
AttributeError: 'Geeks' object has no attribute 'b'

Example 4:  AttributeError can also be raised for a user-defined class when the user misses out on adding tabs or spaces between their lines of code.

Output:

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-c26cd169473f> in <module>
----> 1 d.getkeys()

AttributeError: 'dict_parsing' object has no attribute 'getkeys'

Error:

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-c26cd169473f> in <module>
----> 1 d.getkeys()
AttributeError: 'dict_parsing' object has no attribute 'getkeys'


Solution for AttributeError

Errors and exceptions in Python can be handled using exception handling i.e. by using try and except in Python. 
 

Example: Consider the above class example, we want to do something else rather than printing the traceback Whenever an AttributeError is raised.

Output:

GeeksforGeeks
There is no such attribute


Note: To know more about exception handling click here.

Comment