Python hasattr() Function
The python hasattr() function returns true if an object has given named attribute. Otherwise, it returns false.
object: It is an object whose named attribute is to be checked.
attribute: It is the name of the attribute that you want to search.
It returns true if an object has given named attribute. Otherwise, it returns false.
Python hasattr() Function Example 1
The below example shows the working of hasattr().
Employee has age?: True Employee has salary?: False
Explanation: In the above example, we have created a class named as Employee, then we create the object of the Employee class, i.e., employee. The hasattr(employee, 'age') returns the true value as the employee object contains the age named attribute, while hasattr(employee, 'salary')) returns false value as the employee object does not contain the salary named attribute.