Re: Curious UnboundLocalError Behavior
I had tried the global prefix in the real code (vs. the contrived example in the post), but in the incorrect code block, which made me think something else was up. Yes, these were supposed to be constants that under rare circumstances were changed ;) In the end, I scrapped the rebind approach, because that wasn't the behavior I wanted either.Thanks for the help. - mdf On 1 Mar 2007 00:20:05 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > On 28 fév, 18:15, "Matthew Franz" <[EMAIL PROTECTED]> wrote: > > I'm probably fundamentally misunderstanding the way the interpreter > > works with regard to scope, but is this the intended behavior... > > > (snip traceback) > > > import os,sys > > > > SOMEGLOBAL=1 > > > > def foo(): > > dome=False > > if dome: > > SOMEGLOBAL = 0 > > > This makes SOMEGLOBAL local !-) > > Look for the 'global' statement. Or better, try to avoid rebinding > globals from within a function. > > As as side note: by convention, ALL_UPPER names denote constants. > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matthew Franz http://www.threatmind.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Curious UnboundLocalError Behavior
On 28 fév, 18:15, "Matthew Franz" <[EMAIL PROTECTED]> wrote: > I'm probably fundamentally misunderstanding the way the interpreter > works with regard to scope, but is this the intended behavior... > (snip traceback) > import os,sys > > SOMEGLOBAL=1 > > def foo(): > dome=False > if dome: > SOMEGLOBAL = 0 This makes SOMEGLOBAL local !-) Look for the 'global' statement. Or better, try to avoid rebinding globals from within a function. As as side note: by convention, ALL_UPPER names denote constants. -- http://mail.python.org/mailman/listinfo/python-list
Re: Curious UnboundLocalError Behavior
> 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 -- http://mail.python.org/mailman/listinfo/python-list