Won't use global symbol table?

Rob Nikander nikander at mindspring.com
Tue May 22 19:41:24 EDT 2001


Shouldn't I be able to say this is a module...

x = None
def f():
    # global x
    if not x:
        x = 1
    return x    

Unless I uncomment that global line I get the error:

File "ubtest.py", line 6, in f
    if not x:
UnboundLocalError: local variable 'x' referenced before assignment

I thought python looked for a variable in the local symbol table, and if
it couldn't find it there, went to the global (to the module) symbol
table.  Why do I have to explicitly tell it to do so in this instance?

I am using version 2.1 on Linux.

Thanks,
Rob



More information about the Python-list mailing list