Python hasattr() Function
Instance
Check if the "Person" object has the "age" attribute:
class Person: name = "Bill" age = 63 country = "USA" x = hasattr(Person, 'age')
Definition and Usage
If the specified object has the specified attribute, the hasattr() function will return True, otherwise it will return False.
Syntax
hasattr(object, attribute)
Parameter Value
Parameter | Description |
---|---|
object | Required. Object. |
attribute | You need to check the attribute name that exists. |
Related Pages
Reference Manual:delattr() Function(Delete attribute)
Reference Manual:getattr() Function(Get attribute value)
Reference Manual:setattr() Function(Set attribute value)