intricated functions: how to share a variable

TP Tribulations at Paralleles.invalid
Wed Aug 5 06:13:00 EDT 2009


Hi everybody,

See the following example:

#########
def tutu():

    def toto():

        print a
        a = 4
        print a

    a=2
    toto()

tutu()
##########

I obtain the following error:
"UnboundLocalError: local variable 'a' referenced before assignment"

This is because Python looks in the local context before looking in the
global context.

The use of "global a" in toto() does not help because global allows to force
Python to look for the variable at the module level.

So, how to share a variable between intricated functions?

Thanks a lot

Julien
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)



More information about the Python-list mailing list