Facebook Page Twitter Page LinkedIn Page
× Python Tutorial Python Advanced Python Resources Python Libraries

The Python hasattr() function is used to check whether the specified object has specified attribute or not. It returns true when specified object has specified attribute, else returns false.


hasattr(object, attribute)


object Required. An object
attribute Required. Specify attribute which need to checked in object.


In the below example, hasattr() function is used to check the specified attribute is present in the given class or not.

class MyClass:
   name = 'John'
   age = 25
   city = 'London'

print(hasattr(MyClass, 'age'))
print(hasattr(MyClass, 'hobby'))

The output of the above code will be:


❮ Python Built-in Functions