How to check if an object has an attribute in Python

Today in this tutorial we will focus to check if an object has an attribute or not in Python. We all know that Python is an object-oriented programming language and it consists of objects and classes.

An object is a unit of code that comprises of data(variables that hold the data) and methods whereas a class is a user-defined model for creating objects. The class contains several attributes. The attributes include the class variables that are used to store the data and methods that are the functionalities defined inside the class.

In this tutorial, we will check how to find out if an object has an attribute using an in-built function and some simple examples.

Use the Python in-built function hasattr() to check if an object has an attribute

hasattr() is an in-built Python function that checks if an object has an attribute or not. It takes the name of the object and the attribute to be checked as parameters.

Syntax: hasattr(Object, Attribute)
Object: The object in which we are checking for an attribute.
Attribute: The attribute which we are checking if present in the particular object.

Returns: The hasattr() function returns the following values:
True – If the object has a particular attribute.
False – If the object does not have the particular attribute.

We will see the working of this function using some examples:
Example 1:

class Student:
  name = "Raj"
  id = 36
print('Does the object,Student has an attribute named id: ',hasattr(Student, 'id'))

Output:

Does the object, Student has an attribute named id: True

In this code, we have defined a class named Student, that has two attributes name and id. We use hasattr() function to check if the object student has the attribute named id.
As this object contains the attribute named id, it returns True.

Example 2:

class Student:
  name = "Raj"
  id = 36
print('Does the object,Student has an attribute named age: ',hasattr(Student, 'age'))

Output:

Does the object, Student has an attribute named age: False.

In this example, we have taken the same code as before and checked for the attribute named age using the hasattr() function. Now as the object does not contain any such attribute, it returns False.

There is another way to check if an object contains a particular attribute as explained through the following code.

class Student:
  name = "Raj"
  id = 36
try:
    print('id:',Student.id)
    print('age: ',Student.age)
except AttributeError:
    print('no such attribute present')

Output:

id: 36
no such attribute present

In this above example we use to try and except to check if an attribute is present in the following object. First we check for the attribute named id and then we check for the attribute named age by putting the two statements in the try block and use AttributeError in the except block.

As the attribute id is present, it gets printed while the attribute age is not present hence it goes to the except block and prints the statement present in the except block. To learn more about try and except, visit:  Handling Exceptions using try and except in Python 3.x. or earlier.

Though this method also gives us correct results still it is more convenient to use the in-built function hasattr() as for the cases when the attribute is likely to be not present the hasattr() function is a better choice.

 

Leave a Reply

Your email address will not be published. Required fields are marked *