Curious UnboundLocalError Behavior

Discussion in 'Python' started by Matthew Franz, Feb 28, 2007.

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



    --
    Matthew Franz
    http://www.threatmind.net/
     
    Matthew Franz, Feb 28, 2007
    #1
    1. Advertising

  2. Matthew Franz

    Guest

    On 28 fév, 18:15, "Matthew Franz" <> 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.
     
    , Mar 1, 2007
    #2
    1. Advertising

  3. 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,
    <> wrote:
    > On 28 fév, 18:15, "Matthew Franz" <> 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/
     
    Matthew Franz, Mar 1, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    443
  2. mark

    Curious string behavior

    mark, Jan 28, 2004, in forum: Python
    Replies:
    2
    Views:
    308
    OKB (not okblacke)
    Jan 28, 2004
  3. BartlebyScrivener

    curious paramstyle qmark behavior

    BartlebyScrivener, Oct 20, 2006, in forum: Python
    Replies:
    7
    Views:
    357
    Jon Clements
    Oct 21, 2006
  4. Jim B. Wilson

    The curious behavior of integer objects

    Jim B. Wilson, Jan 15, 2007, in forum: Python
    Replies:
    15
    Views:
    394
    Carl Banks
    Jan 16, 2007
  5. Boris Borcic
    Replies:
    3
    Views:
    386
    Raymond Hettinger
    Apr 21, 2007
Loading...

Share This Page