Python hasattr() Function

Example

Check if the "Person" object has the "age" attribute:

class Person:
  name = "Bill"
  age = 63
  country = "USA"
x = hasattr(Person, 'age')

Run Instance

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 if the attribute name exists.

Related Pages

Reference Manual:delattr() Function(Delete attribute)

Reference Manual:getattr() Function(Get attribute value)

Reference Manual:setattr() Function(Set attribute value)