Why can't I set a global variable in Python?

Viewed 57212

How do global variables work in Python? I know global variables are evil, I'm just experimenting.

This does not work in python:

G = None

def foo():
    if G is None:
        G = 1

foo()

I get an error:

UnboundLocalError: local variable 'G' referenced before assignment

What am I doing wrong?

4 Answers
Related