Globals in nested functions

Stef Mientki S.Mientki-nospam at mailbox.kun.nl
Thu Jun 21 13:41:05 EDT 2007


jm.suresh at no.spam.gmail.com wrote:
> def f():
>     a = 12
>     def g():
>         global a
>         if a < 14:
>             a=13
>     g()
>     return a
> 
> print f()
> 
> This function raises an error. Is there any way to access the a in f()
> from inside g().
> 
> I could find few past discussions on this subject, I could not find
> the simple answer whether it is possible to do this reference.
> 
> -
> Suresh
> 

As I'm struggling with this myself at the moment,
this will do the job:

def f():
     global a
     a = 12

cheers,
Stef Mientki



More information about the Python-list mailing list