To check the existence of a local variable:

myVar = 10

if 'myVar' in locals():
	print(myVar)
else:
	print('None')

if 'myNum' in locals():
	print(myNum)
else:
	print('None')

To check the existence of a global variable:

myVar = 10

if 'myVar' in globals(): print(myVar)
else: print('None')

if 'myNum' in globals(): print(myNum)
else: print('None')

To check if an object has an attribute:

if hasattr(obj, 'attr\_name'): print(obj)
else: print('None')

#References
https://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists

'LANGUAGE > Python' 카테고리의 다른 글

Statistical Visualization or Data Visualization  (0) 2019.08.23
Python Tutorials  (0) 2019.08.23
Screen Record via CV2 and MSS  (0) 2019.07.30
Send Telegram Message via Telegram Bot  (0) 2019.07.16
Reload Module and Function in PyThon  (0) 2019.07.16

+ Recent posts