Methods and Functions

Python hasattr()

The objects contained within the class can be thought of as a blueprint for a class that depicts its attributes and behavior. There might arise a situation where we may need to check the presence of an attribute that is occupied or contained by a class. The Python hasattr() inbuilt utility function can be used to do the same thing. It assists in determining the presence of an attribute in a class.

Python hasattr() function

Definition

  • Python hasattr() function is used to return value TRUE after checking whether the object has the given attribute, else return FALSE.
  • The Python hasattr() built-in function is used to check if the specified object has the named attribute present within a class or not.

Python hasattr() Syntax

Python class represents various attributes in their objects. The hasattr() method returns a Boolean value, either True or False, depending on whether these attributes are present in the class.

The syntax for Python hasattr() function is as follows:

                    

hasattr(object, attribute_name)

hasattr() Parameters

The hasattr() function accepts 2 arguments:

object – can be any object whose named attribute is to be checked

attribute_name – the name of the attribute that is to be searched. This value should always be a string.

Return value from hasattr()

Python hasattr() returns the following Boolean values:

  • TRUE – if the specified named attribute is found in the object
  • FALSE – if the object does not contain the specified named attribute

Example 1: How does hasattr() work in Python?

Example 1

                    

# Python program to illustrate hasattr()
# declaring class
class Car:
    brand = 'Audi'
    price = 1500000
    color = 'White'

# initializing object
c = Car()

# using hasattr() to check named attributes in object
print('Does Car have brand? :', hasattr(c, 'brand'))
print('Does Car have speed? :', hasattr(c, 'speed'))
print('Does Car have features? :', hasattr(c, 'features'))
print('Does Car have color? :', hasattr(c, 'color'))

Output

                    

Does Car have brand? : True
Does Car have speed? : False
Does Car have features? : False
Does Car have color? : True

Explanation

In the above example, we declared a class called Car, and then we constructed the object of Car class, i.e., ‘c’. The hasattr(c, ‘brand’) statement returns TRUE because the car object ‘c’ contains the named attribute ‘brand’, whereas hasattr(c,’speed’) returns FALSE because the ‘c’ object does not have the ‘speed’ named attribute.

Let us look at another example to understand the Python hasattr() function.

Example 2

                    

# Python program to illustrate hasattr()
# declaring class
class Book:
    title = 'Above and Beyond'
    edition = 3

# initializing object
b = Book()

# using hasattr() to check named attributes in object
print('Book has title? :', hasattr(b, 'title'))
print('Book has author? :', hasattr(b, 'author'))

Output

                    

Book has title? : True
Book has author? : False

Applications of hasattr()

  • When attempting to access an attribute of a dynamic object, you can use hasattr() to avoid accessing errors.
  • To conditionally assign a value to a variable, use hasattr() in a ternary operator, as in: age = object.age  if hasattr(object, ‘age’) else 0
  • However, be cautious when using hasattr() because it always returns FALSE, regardless of the error message. As a result, it may obscure a mistake that is distinct from the error that displays if the property does not exist. So, while the attribute may exist, attempting to access it will result in a FALSE result.
  • Chaining hasattr() is occasionally used to avoid entering one associated attribute if the other is not present.

Frequently Asked Questions

Q1. What is hasattr in Python?

There might arise a situation where we may need to check the presence of an attribute that is occupied or contained by a class. The Python hasattr() inbuilt utility function assists us in determining the presence of an attribute in a class. Python class represents various attributes in their objects. The hasattr() method returns a Boolean value, either True or False, depending on whether these attributes are present in the class.

Q2. How do you use hasattr in Python?

The syntax for Python hasattr() function is as follows:

                    

hasattr(object, attribute_name)

The hasattr() function accepts 2 arguments:

object – can be any object whose named attribute is to be checked

attribute_name – name of the attribute that is to be searched. This value should always be a string.

Python hasattr() returns the following Boolean values:

  • TRUE – if the specified named attribute is found in the object
  • FALSE – if the object does not contain the specified named attribute

Example

                    

# declaring class
class Student:
    name = 'Sean'
    age = 22

# initializing object
obj = Student()

# using hasattr() to check named attributes in object
print('Student has name? :', hasattr(obj, 'name'))
print('Student has age? :', hasattr(obj, 'age'))
print('Student has address? :', hasattr(obj, 'address'))
print('Student has mobile? :', hasattr(obj, 'mobile'))

Output

                    

Student has name? : True
Student has age? : True
Student has address? : False
Student has mobile? : False

Q3. What is hasattr used for?

Python hasattr() function has multiple applications. A few of these applications are:

  • When attempting to access an attribute of a dynamic object, you can use hasattr() to avoid accessing errors.
  • Chaining hasattr() is occasionally used to avoid entering one associated attribute if the other is not present.
  • To conditionally assign a value to a variable, use hasattr() in a ternary operator, as in: age = object.age  if hasattr(object, ‘age’) else 0
  • However, be cautious when using hasattr() because it always returns FALSE, regardless of the error message. As a result, it may obscure a mistake that is distinct from the error that displays if the property does not exist. So, while the attribute may exist, attempting to access it will result in a FALSE result.
Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.