getattr(obj, 'attr')
will get an attribute attrfrom objor raise AttributeErrorif it does not exist. You can also specify a default value:
getattr(obj, 'attr', None)
and in this case, the default value will be returned instead of throwing an exception if the attribute is not found on the object.
source
share