Curious UnboundLocalError Behavior

M

Matthew Franz

I'm probably fundamentally misunderstanding the way the interpreter
works with regard to scope, but is this the intended behavior...

franz-macbook:~ mdfranz$ python unboundlocal.py
('Darwin', 'franz-macbook.local', '8.8.5', 'Darwin Kernel Version
8.8.5: Mon Dec 11 19:39:17 PST 2006;
root:xnu-792.16.5.obj~1/RELEASE_I386', 'i386')
2.4.3 (#1, Feb 24 2007, 23:01:32)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]
{'__builtins__': <module '__builtin__' (built-in)>, '__file__':
'unboundlocal.py', 'SOMEGLOBAL': 1, 'sys': <module 'sys' (built-in)>,
'__name__': '__main__', 'foo': <function foo at 0x42cf0>, 'os':
<module 'os' from
'/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/os.pyc'>,
'__doc__': None}
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


Where unboundlocal.py is...

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()

Is SOMEGLOBAL is some weird in-between state, since it is referenced
within foo() but not actually assigned?

If I set dome to True SOMEGLOBAL gets overriden (as I would have expected)

franz-macbook:~ mdfranz$ python unboundlocal.py
('Darwin', 'franz-macbook.local', '8.8.5', 'Darwin Kernel Version
8.8.5: Mon Dec 11 19:39:17 PST 2006;
root:xnu-792.16.5.obj~1/RELEASE_I386', 'i386')
2.4.3 (#1, Feb 24 2007, 23:01:32)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]
{'__builtins__': <module '__builtin__' (built-in)>, '__file__':
'unboundlocal.py', 'SOMEGLOBAL': 1, 'sys': <module 'sys' (built-in)>,
'__name__': '__main__', 'foo': <function foo at 0x42cf0>, 'os':
<module 'os' from
'/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/os.pyc'>,
'__doc__': None}
SOMEGLOBAL: 0
 
B

bruno.desthuilliers

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.
 
M

Matthew Franz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top