Python hasattr() Function

Returns true if the object has the specified property or method.

Syntax

Python
hasattr(object, name)

Parameters

ParameterDescription
object Required. The object to check
name Required. The name of the attribute to check.

Example

Python
class Person:
    firstName = 'Jane'
    lastName = 'Goodall'

    def fullName():
        return firstName + ' ' + lastName

print('Person has firstName property: ', end = '')
print(hasattr(Person, 'firstName'))

print('Person has age property: ', end = '')
print(hasattr(Person, 'age'))

print('Person has fullName method: ', end = '')
print(hasattr(Person, 'fullName'))

Output

Person has firstName property: True
Person has age property: False
Person has fullName method: True