How to check if a variable exists in Python

To check if a variable exists in Python, you can use the snippet below.

Sample Python

if 'myVariable' in locals(): # myVariable exists locally.
if 'myVariable' in globals(): # myVariable exists globally.

Please Share us:
Please rate this snippet

4 thoughts on “How to check if a variable exists in Python

Leave a Reply