Assignment in parent scope w/nested_scopes

Jeff Epler jepler at
Thu May 3 18:28:49 EDT 2001

Is there some way, like "global" makes a name reside in the global scope,
to make a name reside in the parent scope, so that code like
    >>> def f(initial = None):
    ...     x = initial
    ...     def getter(): return x
    ...     def setter(y):
    ...        # magic statement here to make x refer to f.x
    ...        x=y
    ...     return (getter, setter)
will work?  i.e.,
    >>> g, s = f()
    >>> g(0)
    >>> s(1)
    >>> g()

I realize that this can be done with classes, but I'd like to see it done
with nested scopes.

By the way, 'x = [initial]' is a cute trick but also not quite what I'm
looking for.


More information about the Python-list mailing list