title: How do I get the string representation of a variable in python?
body: <p>I have a variable x in python. How can i find the string 'x' from the variable. Here is my attempt:</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>The results are:</p>
  
  <pre><code>Global var x = 123
  Local var x = 321
  </code></pre>
  
  <p>The above recipe seems a bit un-pythonesque. Is there a better/shorter way to achieve the same result? </p>