Python hasattr() function
Check whether an object has an attribute.
Syntax
hasattr(obj, name)
Returns True or False instead of raising AttributeError.
Parameters
| obj | the object |
| name | the attribute name, as a string |
Return value
bool
Example
Code
print(hasattr("text", "upper"), hasattr("text", "append")) print(hasattr([], "append"))
Output
True False True
Related
Learn it step by step in the free lesson Classes & Objects, with exercises that check your answers.