Errors are an essential part of a programmer’s life. And it is not at all bad if you get an error. Getting error means you are learning something new. But we need to solve those errors. And before solving that error, we should know why we are getting that error. There are some commonly occurred errors in python like Type Error, Syntax Error, Key Error, Attribute error, Name Error, and so on.
In this article, we will learn about what is python Attribute Error, why we get it, and how we resolve it? Python interpreter raises an Attribute Error when we try to call or access an attribute of an object, but that object does not possess that attribute. For example- If we try using upper() on an integer, we will get an attribute error.
Why we Get Attribute Error?
Whenever we try to access an attribute that is not possessed by that object, we get an attribute error. For example- We know that to make the string uppercase, we use the upper().
AttributeError: 'int' object has no attribute 'upper'
Here, we are trying to convert an integer to an upper case letter, which is not possible as integers do not attribute being upper or lower. But if try using this upper() on a string, we would have got a result because a string can be qualified as upper or lower.
Some Common Mistakes which result in Attribute error in python
If we try to perform append() on any data type other than List:
Sometimes when we want to concatenate two strings we try appending one string into another, which is not possible and we get an Attribute Error.
string1="Ashwini" string2="Mandani" string1.append(string2)
AttributeError: 'str' object has no attribute 'append'
Same goes with tuples,
AttributeError: 'tuple' object has no attribute 'append'
Trying to access attribute of Class:
Sometimes, what we do is that we try to access attributes of a class which it does not possess. Let us better understand it with an example.
Here, we have two classes- One is Person class and the other is Vehicle class. Both possess different properties.
class Person: def __init__(self,age,gender,name): self.age=age self.gender=gender self.name=name def speak(self): print("Hello!! How are you?") class Vehicle: def __init__(self , model_type , engine_type): self.model_type = model_type self.engine_type = engine_type def horn(self): print("beep!! beep") ashwini=Person(20,"male","ashwini") print(ashwini.gender) print(ashwini.engine_type)
male AttributeError: 'Person' object has no attribute 'engine_type'
AttributeError: 'Person' object has no attribute 'horn'
car=Vehicle( "Hatchback" , "Petrol" ) print(car.engine_type) print(car.gender)
Petrol AttributeError: 'Vehicle' object has no attribute 'gender'
Error- AttributeError: 'Vehicle' object has no attribute 'speak'
In the above examples, when we tried to access the gender property of Person Class, we were successful. But when we tried to access the engine_type() attribute, it showed us an error. It is because a Person has no attribute called engine_type. Similarly, when we tried calling engine_type on Vehicle, we were successful, but that was not in the case of gender, as Vehicle has no attribute called gender.
We get NoneType Error when we get ‘None’ instead of the instance we are supposing we will get. It means that an assignment failed or returned an unexpected result.
name=None i=5 if i%2==0: name="ashwini" name.upper()
AttributeError: 'NoneType' object has no attribute 'upper'
While working with Modules:
It is very common to encounter an attribute error while working with modules. Suppose, we are importing a module named hello and trying to access two functions in it. One is print_name() and another is print_age().
def print_name(): print("Hello! The name of this module is module1") import hello hello.print_name() hello.print_age()
Hello! The name of this module is module1 AttributeError: module 'hello' has no attribute 'print_age'
As the module hello does not contain print_age attribute, we got an Attribute error. In the next section, we will learn how to resolve this error.
How to Resolve Attribute Error in Python
The developers of python have tried to solve any possible problem faced by Python programmers. In this case, too, if we are getting confused, that whether a particular attribute belongs to an object or not, we can make use of help(). For example, if we don’t know whether we can use append() on a string, we can print(help(str)) to know all the operations that we can perform on strings. Not only these built-in data types, but we can also use help() on user-defined data types like Class.
For example- if we don’t know what attributes does class Person that we declared above has,
Isn’t it great! These are precisely the attributes we defined in our Person class.
Now, let us try using help() on our hello module inside the hi module.
import hello help(hello)
Help on module hello: NAME hello FUNCTIONS print_name()
Using Try – Except Statement
A very professional way to tackle not only Attribute error but any error is by using try-except statements. If we think we might get an error in a particular block of code, we can enclose them in a try block. Let us see how to do this.
Suppose, we are not sure whether Person class contain engine_type attribute or not, we can enclose it in try block.
class Vehicle: def __init__(self , model_type , engine_type): self.model_type = model_type self.engine_type = engine_type def horn(self): print("beep!! beep") car=Vehicle( "Hatchback" , "Petrol" ) try: print(car.engine_type) print(car.gender) except Exception as e: print(e)
Petrol 'Vehicle' object has no attribute 'gender'.
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python
Whenever to try to access an attribute of an object that does not belong to it, we get an Attribute Error in Python. We can tackle it using either help() function or try-except statements.
Try to run the programs on your side and let us know if you have any queries.