In this Python built-in functions tutorial, we will go through hasattr() function to check whether a specified object has a specific attribute or not. hastattr function in python returns only a Boolean value that means True or False.
Headings of Contents
Python hasattr() function Introduction
Python hasattr() function is a built-in function that is used to check whether a specified Object has a specified attribute or not. hasattr() function returns a True or False Boolean value. To use hasattr() function in Python, you don’t need to install it or import it from any module or package because it comes with Python default.
Syntax
The syntax of hasattr() function in Python is:-
hasattr(Object, attribute)
Parameter
Python hasattr function takes two parameters:-
- Object:– Required, An Object.
- attribute:- The name of the attribute you want to check if exists.
Return Value
The return value of hasattr() function is True or False.
- True:- if the specified attribute belongs to a specified object
- False:- if the specified attribute does not belong to a specified object
Python hasattr() Function Examples
Here, I am about to explore the Python hasattr() function along with different-different examples.
Example: Using hasattr() function with custom class
class Student: first_name = 'Vishvajit' last_name = 'Rao' roll_no = 120 course = 'BCA' def details(self): print( f"My name is {self.first_name + self.last_name} and my roll no is {self.roll_no}. I am pursuing {self.course}.") print(hasattr(Student, 'first_name')) print(hasattr(Student, 'last_name')) print(hasattr(Student, 'details'))
Output
True
True
True
If you try to check an attribute that does not belong to a specified object, in That case, it returns False. As shown below.
print(hasattr(Student, 'Country')) # Output:- False
Example: Using hasattr() function with Python Dictionary
I have a Python dictionary employee
, and now I am going to check whether a few specified attributes are the attributes of the employee
or not.
# Python dictionary employee = {"name": "Vishvajit Rao", "occupation": "Developer", "email": "[email protected]"} print(hasattr(employee, 'update')) print(hasattr(employee, 'keys')) print(hasattr(employee, 'pop')) print(hasattr(employee, 'items')) print(hasattr(employee, 'values')) print(hasattr(employee, 'append'))
Output
True
True
True
True
True
False
In the above example, attributes ( ‘update’, ‘keys’, ‘pop’, ‘items’, ‘values’ ) are the attributes employee that’s why hasattr() function has returned True but in the case of ‘append’ attribute, it has returned False because it is not an attribute of the employee.
Note:- To display all the attributes of an object, Python provides an amazing built-in function calleddir()
.dir(
) function takes an object as a parameter and returns all its attributes. For example, I am taking both objects employee and Student to display all their attributes.
Attributes of a class Student from the first example:
print(dir(Student))
Output
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'course', 'details', 'first_name', 'last_name', 'roll_no']
Attributes of an objecting employee of the second example:
# Python dictionary employee = {"name": "Vishvajit Rao", "occupation": "Developer", "email": "[email protected]"} print("Attributes of employee:- ", dir(employee))
Output
Attributes of employee:- ['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
Conclusion
In this tutorial, you have learned all about Python hasattr function to check whether a specified object has a specified attribute or not. If the specified object has a specified attribute, Then it will return True otherwise it will return False.
If you like this article, please keep visiting for further python built-in functions tutorials.
Other Python Built-in Functions
For More Information:- Click Here