Weird error in python regarding scoping : <pre><code>x = 5
def foobar():
    print (x) #Prints global value of x, which is 5
    x = 1 #assigns local variable x to 1
foobar()
</code></pre>

<p>Instead, it throws a    </p>

<pre><code>UnboundLocalError: local variable 'x' referenced before assignment
</code></pre>

<p>What am I misunderstanding in the comments? Note, I understand if i do x=x+1, it'll throw error due to 'accessing value of local scope x beforei it's defined', but in this case I'm doing x=1, which does not require reading of existing value of x! This is NOT a duplicate question.</p>
