Curious UnboundLocalError Behavior
Daniel Nogradi
nogradi at gmail.com
Wed Feb 28 12:33:35 EST 2007
> I'm probably fundamentally misunderstanding the way the interpreter
> works with regard to scope, but is this the intended behavior...
>
[....]
> SOMEGLOBAL:
> Traceback (most recent call last):
> File "unboundlocal.py", line 15, in ?
> foo()
> File "unboundlocal.py", line 11, in foo
> print "SOMEGLOBAL:",SOMEGLOBAL
> UnboundLocalError: local variable 'SOMEGLOBAL' referenced before assignment
[......]
> import os,sys
>
> SOMEGLOBAL=1
>
> def foo():
> dome=False
> if dome:
> SOMEGLOBAL = 0
>
> print globals()
> print "SOMEGLOBAL:",SOMEGLOBAL
>
> print os.uname()
> print sys.version
> foo()
>
Try:
import os,sys
SOMEGLOBAL=1
def foo():
global SOMEGLOBAL
dome=False
if dome:
SOMEGLOBAL = 0
print globals()
print "SOMEGLOBAL:",SOMEGLOBAL
print os.uname()
print sys.version
foo()
HTH,
Daniel
More information about the Python-list
mailing list