title: Accessing the outer scope in Python 2.6
body: <p>Say, I have some scope with variables, and a function called in this scope wants to change some immutable variables:</p>
  
  <pre>
  def outer():
      s = 'qwerty'
      n = 123
      modify()
  
  def modify():
      s = 'abcd'
      n = 456
  </pre>
  
  <p>Is it possible somehow to access the outer scope? Something like <code>nonlocal</code> variables from Py3k.</p>
  
  <p>Sure I can do <code>s,n = modify(s,n)</code> in this case, but what if I need some generic 'injection' which executes there and must be able to reassign to arbitrary variables?</p>
  
  <p>I have performance in mind, so, if possible, <code>eval</code> &amp; stack frame inspection is not welcome :)</p>
  
  <hr>
  
  <p><strong>UPD</strong>: It's impossible. Period. However, there are some options how to access variables in the outer scope:</p>
  
  <ol>
  <li>Use globals. By the way, <code>func.__globals__</code> is a mutable dictionary ;)</li>
  <li>Store variables in a dict/class-instance/any other mutable container</li>
  <li>Give variables as arguments &amp; get them back as a tuple: <code>a,b,c = innerfunc(a,b,c)</code></li>
  <li>Inject other function's bytecode. This is possible with <code>byteplay</code> python module.</li>
  </ol>