How to check whether a local variable exists in Python

1 Answer

0 votes
def f():
    s = "local variable" 
    
    if 's' in locals():
       print ('Variable exist')
    else:
        print ('Variable not exist')

f()
    
    
    
'''
run:

Variable exist

'''

 



answered Feb 22, 2021 by avibootz

Related questions

1 answer 152 views
2 answers 155 views
3 answers 330 views
1 answer 152 views
2 answers 185 views
3 answers 258 views
...