title: Python - local variable referenced before assignment for variable that should shadow one outside of scope
body: <p>This code does not work (<code>UnboundLocalError: local variable 'x' referenced before assignment</code>):</p>
  <pre><code>x = 5
  def thing():
      x = x + 1
  thing()
  </code></pre>
  <p>This one does (only changed <code>x</code> to <code>i</code>):</p>
  <pre><code>x = 5
  def thing():
      i = x + 1
  thing()
  </code></pre>
  <p>I expected that with the first one, it would create a new variable called <code>x</code> with a value of 6 (leaving the outside variable unchanged), which is what <code>i</code> is in the second example. What's going on here?
  It seems that when a variable is created for the first time in the scope, it checks to see if it exists in any outer scope and raises an error if it has already been found and raises an error if it has already been found.</p>
  <p>Now, my question is, what is the purpose of this?</p>