How do I get the string representation of a variable in python?
问题 I have a variable x in python. How can i find the string 'x' from the variable. Here is my attempt: def var(v,c): for key in c.keys(): if c[key] == v: return key def f(): x = '321' print 'Local var %s = %s'%(var(x,locals()),x) x = '123' print 'Global var %s = %s'%(var(x,locals()),x) f() The results are: Global var x = 123 Local var x = 321 The above recipe seems a bit un-pythonesque. Is there a better/shorter way to achieve the same result? 回答1: Q: I have a variable x in python. How can i