Hi, I am trying to work out why I get UnboundLocalError when accessing an int from a function where the int is at the global scope, without explicitly declaring it as global but not when accessing a list in similar circumstances.
The documentation: http://docs.python.org/ref/naming.html does not give me enough info to determine why the difference exists as it does not seem to mention types at all.. The code: ===== scope_and_type.py ======= m = 0 n = [0] def int_access0(): m = m + 1 return m def int_access1(): m += 1 return m def list_access0(): n[0] = n[0] + 1 return n def list_access1(): n[0] += 1 return n try: print "\nint_access0:", int_access0() except UnboundLocalError, inst: print " ERROR:\n", inst try: print "\nint_access1:", int_access1() except UnboundLocalError, inst: print " ERROR:\n", inst try: print "\nlist_access0:", list_access0() except UnboundLocalError, inst: print " ERROR:\n", inst try: print "\nlist_access1:", list_access1() except UnboundLocalError, inst: print " ERROR:\n", inst print "\n (m,n) = ", (m,n) p = (0,) def tuple_access(): return p[0] try: print "\ntuple_acces:", tuple_access() except UnboundLocalError, inst: print " ERROR:\n", inst print "\n p = ", p ===== END scope_and_type.py ======= The output: >>> int_access0: ERROR: local variable 'm' referenced before assignment int_access1: ERROR: local variable 'm' referenced before assignment list_access0: [1] list_access1: [2] (m,n) = (0, [2]) tuple_acces: 0 p = (0,) >>> -- http://mail.python.org/mailman/listinfo/python-list