python first assignment of a global variable

Rodrigue rodriguealcazar at gmail.com
Wed Jul 15 13:55:34 EDT 2009


Hi all,

I came accross a strange behaviour in python today. Here is a simple
example to describe my situation:

MY_GLOBAL = ''

def a():
    print 'global is: ', MY_GLOBAL

def b():
    try:
        MY_GLOBAL += 'bla'
    except Exception, e:
        print 'b: ', e

def c():
    try:
        global MY_GLOBAL
        MY_GLOBAL += 'bla'
    except Exception, e:
        print 'c: ', e

def d():
    try:
        if not MY_GLOBAL:
            print 'not my global'
    except Exception, e:
        print 'd: ', e

def e():
    try:
        if not MY_GLOBAL:
            MY_GLOBAL = ''
    except Exception, e:
        print 'e: ', e

def f():
    global MY_GLOBAL
    if not MY_GLOBAL:
        MY_GLOBAL = ''

def e_raise():
    if not MY_GLOBAL:
        MY_GLOBAL = 'bla'

if __name__ == "__main__":
    a()
    b()
    c()
    d()
    e()
    f()
    e_raise()


And here is the output I get:

global is:
b:  local variable 'MY_GLOBAL' referenced before assignment
e:  local variable 'MY_GLOBAL' referenced before assignment
Traceback (most recent call last):
  File "glo.py", line 49, in <module>
    e_raise()
  File "glo.py", line 39, in e_raise
    if not MY_GLOBAL:
UnboundLocalError: local variable 'MY_GLOBAL' referenced before
assignment


Now, I was reading that page http://stackoverflow.com/questions/370357/python-variable-scope-question
and found (understood) only part of the behaviour that could explain
the output.

Basically, I was very surprised to discover that e() raises an
exception, but even more that e_raise() points to
if not MY_GLOBAL Is the problem not really when I assign?

My assumption is that some reordering is happening behind the scenes
that creates a situation similar to the += which assigns hence expects
to be at the local level.

I would love some enlightenment here

Rodrigue



More information about the Python-list mailing list