In every programming language, if we develop new programs, there is a high chance of getting errors or exceptions. These errors yields to the program not 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 assined a value 10. In this process suppose we want to append another value to that variable. It’s not possible. Because the variable is a integer type it’s not support 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. Than their is no problem and not getting”Attribute error”.
Note: Attribute errors in Python are generally raised when an invalid attribute reference is made.
There is a few chances of getting AttributeError.
Example 1:
# Python program to demonstrate # AttributeError X = 10 # Raises an AttributeError X.append(6) |
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.
# Python program to demonstrate # AttributeError # Raises an AttributeError as there is no # method as fst for strings string = "The famous website is { }".fst("geeksforgeeks") print(string) |
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 user-defined class when the user tries to make an invalid attribute reference.
# Python program to demonstrate # AttributeError class Geeks(): def __init__(self): self.a = 'GeeksforGeeks' # Driver's code obj = Geeks() print(obj.a) # Raises an AttributeError as there # is no attribute b print(obj.b) |
Output:
GeeksforGeeks
Traceback (most recent call last):
File "/home/373989a62f52a8b91cb2d3300f411083.py", line 17, in
print(obj.b)
AttributeError: 'Geeks' object has no attribute 'b'
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.
# Python program to demonstrate # AttributeError class Geeks(): def __init__(self): self.a = 'GeeksforGeeks' # Driver's code obj = Geeks() # Try and except statement for # Exception handling try: print(obj.a) # Raises an AttributeError print(obj.b) # Prints the below statement # whenever an AttributeError is # raised except AttributeError: print("There is no such attribute") |
Output:
GeeksforGeeks There is no such attribute
Note: To know more about exception handling click here.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python program to check if the list contains three consecutive common numbers in Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet

