nested function scopes

Emile van Sebille emile at fenx.com
Sun Dec 14 16:07:38 CET 2003


Terry Reedy:
>
> Nils Grimsmo:
> > i'm having some trouble nesting functions. consider the following:
[snip]
> > def f():
> >     x = 1
> >     def g():
> >         print x # this is not ok
> >         x = 2 # this implies that x is local to g
[snip]
> > how do i declare that x belongs to the parent function, so that i
can do
> > assignments to it?
>
> Currently you cannot.
>

Why not use the old method of passing variables to nested functions:
def f()
    x = 1
    def g(x=x):
        print x
        x = 2

Emile van Sebille
emile at fenx.com





More information about the Python-list mailing list