Won't use global symbol table?
Brian Quinlan
BrianQ at ActiveState.com
Tue May 22 20:26:53 EDT 2001
> 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?
You can do anything that you want to a global without declaring it as such,
except for reassignment. A complete description of the global statement is
given at:
http://www.python.org/doc/current/ref/global.html
--
Brian Quinlan
Python and XSLT Developer
BrianQ at ActiveState.com
Use the power of the dragon for your programming needs.
Download Komodo, our Mozilla-based, cross-platform IDE.
http://www.ActiveState.com/Komodo
More information about the Python-list
mailing list