This hasattr()
function returns a Boolean stating whether the object has the specified attribute.
hasattr(object, name)
Here,
>>> 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
.