To check the existence of a local variable:
1 2 |
if 'myVar' in locals(): # myVar exists. |
To check the existence of a global variable:
1 2 |
if 'myVar' in globals(): # myVar exists. |
To check if an object has an attribute:
1 2 |
if hasattr(obj, 'attr_name'): # obj.attr_name exists. |