I am trying to extract the name of a variable as a string. I have defined two variables CurrentField and PreviousField and want to extract these names as strings to use elsewhere in my code.
CurrentField = ['One', 'Two']
PreviousField = ['One0', 'Two0']
def Updatedict(varname):
callers_local_vars = inspect.currentframe().f_back.f_locals.items()
stringvar = [k for k, v in callers_local_vars if v is varname][0]
return stringvar
listofvars = [CurrentField, PreviousField]
ParameterDict = [Updatedict(keyname) for keyname in listofvars]
ParameterDict
All I get is a list saying ['keyname','keyname'] and what I am looking for is ['CurrentField','PreviousField'] .
How do I get my desired output?
Thanks a lot in advance.