Python hasattr() built-in function
From the Python 3 documentation
The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not.)
Introduction
The hasattr()
function checks if an object has a given attribute. It takes the object and the attribute name (as a string) as arguments and returns True
if the attribute exists, and False
otherwise.
Example
class Person:
name = "John"
age = 30
p = Person()
print(hasattr(p, 'name')) # Output: True
print(hasattr(p, 'age')) # Output: True
print(hasattr(p, 'email')) # Output: False