How to check if a variable exists in Python

2 Answers

0 votes
def f_local():
    s = "python"
    if 's' in locals():
        print ('s local variable exists')
    else:
        print ('s local variable does not exist')
 
f_local()
 
 
   
   
   
   
'''
run:
   
s local variable exists
   
'''

 



answered Apr 15, 2021 by avibootz
0 votes
s = "python"

if 's' in globals():
    print ("s globals variable exist")
else:
    print ("s globals variable does not exist")


  
  
  
  
'''
run:
  
s globals variable exist
  
'''

 



answered Apr 15, 2021 by avibootz

Related questions

1 answer 165 views
1 answer 152 views
1 answer 269 views
1 answer 129 views
1 answer 126 views
1 answer 112 views
...