title: How to get/set local variables of a function (from outside) in Python?
body: <p>If I have a function (in Python 2.5.2) like:</p>
  
  <pre><code>def sample_func():
      a = 78
      b = range(5)
      #c = a + b[2] - x
  </code></pre>
  
  <p>My questions are:</p>
  
  <ol>
  <li>How to get the local variables (a,b) of the function from outside <strong>without</strong> using <em>locals()</em> inside the function? (kind of reflection)</li>
  <li>Is it possible to set a local variable (say <strong><em>x</em></strong>) from outside so that the commented line works? (I know it sounds weird).</li>
  </ol>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>Everyone is asking for a use-case. But it is a weird situation. (Don't blame me, I did not create it). Here is the scenario:</p>
  
  <ol>
  <li>I have an encrypted python source file containing a python function.</li>
  <li>A C extension module decrypts it and builds that function in-memory.</li>
  <li>A main python program first calls the C extension with that encrypted file location.</li>
  <li>Then the main program calls the function that has been built in-memory (by the C extension)</li>
  <li>But main program needs to know the local variables of that function (Dont ask me why, it was not me)</li>
  <li>For some (damn) reason, main program needs to set a variable too (weirdest of all)</li>
  </ol>