+ 4

How we can do this in python?

I have a python code.it contain many variables.and I want to find variable name, it means if python code contain this variable which I want to find it,than it will run otherwise not run.and how i can find the variable present or not present in my Python code.How I can do that in python.

19th May 2018, 4:33 PM
Maninder $ingh
Maninder $ingh - avatar
4 Answers
+ 4
# to find all the local variables if 'var' in locals().keys(): print 'yes' # to find all the global variables if 'var' in globals().keys(): print 'yes'
20th May 2018, 5:25 AM
Lokesh Kumar
Lokesh Kumar - avatar
+ 5
I have a idea. try: print(x) except NameError: print('Variable is not found')
19th May 2018, 4:45 PM
Maninder $ingh
Maninder $ingh - avatar
+ 4
Maninder Singh yeah nice!
19th May 2018, 8:47 PM
Oma Falk
Oma Falk - avatar
+ 3
Self help is the best help!
19th May 2018, 5:15 PM
Just A Rather Ridiculously Long Username