Won't use global symbol table?

k kevin04107 at hotmail.com
Thu May 24 11:12:46 EDT 2001


This code should do what you intended to do:


x = None

def f():
    global x
    try:
        if (x): pass    # throws an exception if x does not exist.
    except:
        x=1
    return x



K.




"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:9efubv023eo at enews1.newsguy.com...
> "Rob Nikander" <nikander at mindspring.com> wrote in message
> news:20010522.194123.1450573622.7698 at kanchenjunga.mindspring.com...
> > Shouldn't I be able to say this is a module...
> >
> > x = None
> > def f():
> >     # global x
> >     if not x:
> >         x = 1
> >     return x
> >
> > Unless I uncomment that global line I get the error:
> >
> > File "ubtest.py", line 6, in f
> >     if not x:
> > UnboundLocalError: local variable 'x' referenced before assignment






More information about the Python-list mailing list