Built-in Functions
hasattr()
Check whether an object has a named attribute.
hasattr(object, name)
hasattr() is a quick way to test for an attribute before trying to use it. It returns a boolean instead of the attribute value.
Parameters
| Name | Description |
|---|---|
| object | The object or class to inspect. |
| name | The attribute name as a string. |
Returns
True if the attribute exists; otherwise False.
Try it
Run it and change it
class User:
name = "Ada"
print(hasattr(User, "age"))