Assigning to global var in Python?
<p>I am trying out Python for a new project, and in that state, need some global variable. To test without changing the hardcoded value, I've tried to make a function that reassign thoses variables:</p>

<pre><code>foo = None;

def setFoo(bar):
    global foo = bar;

setFoo(1);
</code></pre>

<p>However, this get:</p>

<pre><code>  File ".\test.py", line 4
    global foo = bar;
               ^
SyntaxError: invalid syntax
</code></pre>

<p>I've already read <a href="http://stackoverflow.com/questions/10588317/python-function-global-variables">two</a> <a href="http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them">questions</a> higly related to my problem, but obviously missed some gotcha of Python.</p>

<p>Could anyone enlighten me?</p>

<p>Python doesn't require you to explicitly declare variables and automatically assumes that a variable that you assign has function scope unless you tell it otherwise. The <code>global</code> keyword is the means that is provided to tell it otherwise. And you can't assign value when you declare it as global. Correct way will be</p>

<pre><code>foo = None

def setFoo(bar):
    global foo 
    foo = bar

setFoo(1)
</code></pre>

python