When do I need to use the global keyword in python
<p>Okay, so I've had this really annoying problem where a variable got set locally but then outside of that function reverted to it's old self (in this case None), but at the same time I could manipulate other variables and still can without using the "global" keyword.</p>

<p>I can't provide the real code for this but it goes something like this:</p>

<pre><code>foo = {}
foo_foo = {}
bar = None


def changes_foo():
    ...do some stuff to foo...

class EditThread(threading.Thread):

    def __init__(self):
        setup()

    def run(self):
       for key, value in foo.items():
           do_update_task(key, value)

    def do_update_task(self, key, value):
         ...do some editing too foo...
         del foo[key]
         bar = [key, value]
         foo_foo[key] = value

def print_the_bar():
    print bar 
</code></pre>

<p>Please note that all the operations on <code>foo</code> and <code>foo_foo</code> works just fine, but bar is still None when I call <code>print_the_bar</code>, and I've had lots of print statements in my code to verify that bar inside of <code>do_update_task</code> indeed has the correct values and isn't None.</p>

<p>Could someone please explain to me why it is so?</p>
