title: Python: In class Foo: x = MyClass() can MyClass know the name of the variable it is assigned to?
body: <p>I'd like to be able to do the following:</p>
  
  <pre><code>class PrintName:
      def __init__( self, obj ):
          print obj._name
  
  class SetName:
      def __init__( self, name = None ): # Default name is None
          self._name = name
  
  class Test:
      f = SetName( ) # No explicit name given
      g = PrintName( f )
  </code></pre>
  
  <p>At this point I'd like python to print 'f', so by the time PrintName( f ) is executed f should know it's name.</p>
  
  <p>Every auto naming solution I found gave the attributes their names after creation.
  I've been trying to solve this with metaclasses but even that doesn't seem to work.</p>
  
  <p>I'd like to do this to be able to 'save' and 'reload' the python code for later use ( kind of like a primitive scripting language that can be changed in a program with on-use-evaluation )</p>
  
  <p>For example:</p>
  
  <pre><code>x = 0
  y = 0
  p = ( x, y )
  </code></pre>
  
  <p>print p resulting in (0,0), then do something with x and y resulting in</p>
  
  <pre><code>x = 124
  y = -32
  p = ( x, y )
  </code></pre>
  
  <p>print p becoming (124,-32).</p>
  
  <p>The simplest way to do this would be to use</p>
  
  <pre><code>p = ( 'x', 'y' )
  </code></pre>
  
  <p>but in this case how do we know that 'x' is the name of a variable and not the string 'x'</p>
  
  <p>I've tried writing a simple scripting language for this purpose, but that's a lot of work and if the above could work then whole of the python language could be used in the script.</p>
  
  <p>I'm trying to find a simple and flexible solution for my problem.</p>
  
  <p>Thanks in advance for any help.</p>