Curious UnboundLocalError Behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew Franz

    #1

    Curious UnboundLocalError Behavior

    I'm probably fundamentally misunderstandin g 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.p y', 'SOMEGLOBAL': 1, 'sys': <module 'sys' (built-in)>,
    '__name__': '__main__', 'foo': <function foo at 0x42cf0>, 'os':
    <module 'os' from
    '/opt/local/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/os.pyc'>,
    '__doc__': None}
    SOMEGLOBAL:
    Traceback (most recent call last):
    File "unboundlocal.p y", line 15, in ?
    foo()
    File "unboundlocal.p y", line 11, in foo
    print "SOMEGLOBAL:",S OMEGLOBAL
    UnboundLocalErr or: 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:",S OMEGLOBAL

    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.p y', 'SOMEGLOBAL': 1, 'sys': <module 'sys' (built-in)>,
    '__name__': '__main__', 'foo': <function foo at 0x42cf0>, 'os':
    <module 'os' from
    '/opt/local/Library/Frameworks/Python.framewor k/Versions/2.4/lib/python2.4/os.pyc'>,
    '__doc__': None}
    SOMEGLOBAL: 0



    --
    Matthew Franz

  • bruno.desthuilliers@gmail.com

    #2
    Re: Curious UnboundLocalErr or Behavior

    On 28 fév, 18:15, "Matthew Franz" <mdfr...@gmail. comwrote:
    I'm probably fundamentally misunderstandin g 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.

    Comment

    • Matthew Franz

      #3
      Re: Curious UnboundLocalErr or 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, bruno.desthuill iers@gmail.com
      <bruno.desthuil liers@gmail.com wrote:
      On 28 fév, 18:15, "Matthew Franz" <mdfr...@gmail. comwrote:
      I'm probably fundamentally misunderstandin g 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.
      >
      --

      >

      --
      Matthew Franz

      Comment

      Working...