Python hasattr() FunctionThe python hasattr() function returns true if an object has given named attribute. Otherwise, it returns false. SignatureParametersobject: It is an object whose named attribute is to be checked. attribute: It is the name of the attribute that you want to search. ReturnIt returns true if an object has given named attribute. Otherwise, it returns false. Python hasattr() Function Example 1The 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 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India