Python hasattr() Function
Returns true if the object has the specified property or method.
Syntax
Python
Copy Code
hasattr(object, name)
Parameters
Parameter | Description |
---|---|
object |
Required. The object to check |
name |
Required. The name of the attribute to check. |
Example
Python
Copy Code
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