How does Python handle globals?
<p>I've come across some very odd handling of global variables in Python. I was hoping someone can explain and justify these surprises!</p>

<p>A) This code prints 10 as expected:</p>

<pre><code>def func():
  print(a)
a = 10
func()
</code></pre>

<p>B) This code throws an exception about referencing a too early:</p>

<pre><code>def func():
  print(a)
  a += 1
a = 10
func()
</code></pre>

<p>C) But this code prints [10] as expected:</p>

<pre><code>def func():
  print(a)
  a.append(1)
a = [10]
func()
</code></pre>

<p>So I can gather that the type of <code>a</code> changes its scope and additionally later statements that haven't even been reached yet change how <code>a</code> is seen. I know I can use <code>global a</code> at the start of the function but it's rather verbose.</p>

<p>Can anyone tell me what rules Python is using to handle its bizarre scoping?</p>
