1try:
2 thevariable
3except NameError:
4 print("well, it WASN'T defined after all!")
5else:
6 print("sure, it was defined.")
7
1# for local
2if 'myVar' in locals():
3 # myVar exists.
4# for globals
5if 'myVar' in globals():
6 # myVar exists.