title: exec() bytecode with arbitrary locals?
body: <p>Suppose I want to execute code, for example</p>
  
  <pre><code>    value += 5
  </code></pre>
  
  <p>inside a namespace of my own (so the result is essentially <code>mydict['value'] += 5</code>). There's a function <code>exec()</code>, but I have to pass a string there:</p>
  
  <pre><code>    exec('value += 5', mydict) 
  </code></pre>
  
  <p>and passing statements as strings seems strange (e.g. it's not colorized that way). 
  Can it be done like:</p>
  
  <pre><code>    def block():
          value += 5
  
      ???(block, mydict)
  </code></pre>
  
  <p>? The obvious candidate for last line was <code>exec(block.__code__, mydict)</code>, but no luck: it raises <code>UnboundLocalError</code> about <code>value</code>. I believe it basically executes <code>block()</code>, not <em>the code inside block</em>, so assignments aren't easy – is that correct?</p>
  
  <p>Of course, another possible solution would be to disassembly <code>block.__code__</code>...</p>
  
  <p>FYI, I got the question because of <a href="http://mail.python.org/pipermail/python-ideas/2009-August/005552.html" rel="nofollow noreferrer">this thread</a>. Also, this is why some (me undecided) call for new syntax </p>
  
  <pre><code>    using mydict: 
          value += 5
  </code></pre>
  
  <hr>
  
  <p>Note how this doesn't throw error but doesn't change <code>mydict</code> either:</p>
  
  <pre><code>    def block(value = 0):
          value += 5
  
      block(**mydict)
  </code></pre>