ScanSkill

hasattr

This hasattr() function returns a Boolean stating whether the object has the specified attribute.

Syntax

hasattr(object, name)

Here,

  • object: Required. An object.
  • name: Required. Must be a string. Name of the attribute.

Examples

>>> class Class1:
...     pass
...
>>> f = Class1()
>>> hasattr(f, 'x')
False
>>> f.x = 100
>>> hasattr(f, 'x')
True

Here, first, the named attribute x is not present in the class Class1, so the hasattr() method results False. After defining new attribute x for the class Class1, the hasattr() returns True.