Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Curious UnboundLocalError Behavior

Reply
Thread Tools

Curious UnboundLocalError Behavior

 
 
Matthew Franz
Guest
Posts: n/a
 
      02-28-2007
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;
rootnu-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;
rootnu-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



--
Matthew Franz
http://www.threatmind.net/
 
Reply With Quote
 
 
 
 
bruno.desthuilliers@gmail.com
Guest
Posts: n/a
 
      03-01-2007
On 28 fév, 18:15, "Matthew Franz" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Matthew Franz
Guest
Posts: n/a
 
      03-01-2007
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, http://www.velocityreviews.com/forums/(E-Mail Removed)
<(E-Mail Removed)> wrote:
> On 28 fév, 18:15, "Matthew Franz" <(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unboundlocalerror with cgi module David Bear Python 8 04-11-2006 02:18 PM
UnboundLocalError: local variable 'colorIndex' referenced silverburgh.meryl@gmail.com Python 2 02-26-2006 09:29 PM
why UnboundLocalError? Alex Gittens Python 6 07-09-2005 04:56 PM
HELP:UnboundLocalError: local variable '_nntp' referenced before assignment Peter Moscatt Python 2 03-19-2005 08:37 AM
UnboundLocalError on shadowed import Brad Clements Python 3 06-30-2004 12:15 PM



Advertisments