Definition
The hasattr() function returns True
if the specified object has the specified attribute, otherwise False
.
Syntax
hasattr(object, attribute)
Parameters
Parameter | Description |
---|---|
object | Required. An object. |
attribute | The name of the attribute to check if exists |
Examples
class Car:
name = "Nissan"
model = 1987
country = "Japan"
result = hasattr(Car, 'model')
falseResult = hasattr(Car, 'age')
print(result)
print(falseResult)