Assignment in parent scope w/nested_scopes

Jeff Epler jepler at inetnebr.com
Fri May 4 00:28:49 CEST 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)
    0
    >>> s(1)
    >>> g()
    1

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.

Jeff



More information about the Python-list mailing list