Javatpoint Logo
Javatpoint Logo

Python hasattr() Function

The python hasattr() function returns true if an object has given named attribute. Otherwise, it returns false.

Signature

Parameters

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.

Return

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().

Output:

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.


Next TopicPython Functions




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA