hasattr(object, name)
The hasattr() function returns True if an object has the specified attribute, and False if it does not.
This function works by attempting to retrieve the attribute using getattr(), checking for an exception. Use getattr() to return an attribute, setattr() to set one, and delattr() to delete one.
Parameters
hasattr() takes two parameters:
object: the object to check for the named attributename: the name of the attribute to find
Return Value
hasattr() returns:
Trueif the object has the specified attributeFalseif the object does not have the specified attribute
Examples
class Person:
age = 38
name = "John"
person = Person()
print("Age:", hasattr(person, "age"))
print("Salary:", hasattr(person, "salary"))
# Output
# Age: True
# Salary: False