title: Get the name of a specific attribute of some object as a string in Python
body: <p>Once I have a specific attribute of an object belonging to some class in Python, how can I get its name as a string? Is this info contained somewhere?</p>
  
  <p>In the following example, suppose I use <code>a.someval</code> in some function: how can I get its name as a string <code>someval</code> just from this value?</p>
  
  <pre><code>class OneClass:
      def __init__(self):
          self.whatIam = 'I am a OneClass instance'
          self.whatIdo = 'Nothing really useful'
  
      def setSomeVal(self, val):
          self.someval = val
  
  a = OneClass()
  a.setSomeVal(13)
  </code></pre>
  
  <p>If I had the object owning the attribute and not only the attribute value itself, I could go back to its name doing something like:</p>
  
  <pre><code>def present_yourself_knowing_your_owner_object(you, owner):
      print 'My value is', you
      print 'My name is', owner.__dict__.keys()[owner.__dict__.values().index(you)]
  
  present_yourself_knowing_your_owner_object(a.someval, a)
  # My value is 13
  # My name is someval
  present_yourself_knowing_your_owner_object(a.whatIam, a)
  # My value is I am a OneClass instance
  # My name is whatIam
  </code></pre>
  
  <p>And I know we can access a method name like this:</p>
  
  <pre><code>print a.setSomeVal.__name__
  # setSomeVal
  </code></pre>
  
  <p>But would it be possible to have the attribute name just from itself, something like:</p>
  
  <pre><code>def present_yourself(you):
      print 'My value is', you
      print 'and my name is', '&lt; ** YOUR SOLUTION HERE ** &gt;'
  </code></pre>
  
  <hr>
  
  <p><em>EDIT to answer @Daniel Roseman's comment:</em></p>
  
  <p>This can be achieved for some objects, e.g. methods:</p>
  
  <pre><code>def present_yourself(you):
      print 'My value is', you
      print 'and my name is', you.__name__ #'&lt; ** YOUR SOLUTION HERE ;-) ** &gt;'
  present_yourself(a.setSomeVal)
  # My value is &lt;bound method OneClass.setSomeVal of &lt;__main__.OneClass instance at 0x7f3afe255f38&gt;&gt;
  # and my name is setSomeVal
  </code></pre>