global in nested functions (viper)

Evan Simpson evan at tokenexchange.com
Tue Sep 21 16:30:34 EDT 1999


This sounds relevent to one of my many proposals to make work for other
folks <wink>.  Perhaps instead of causing a reference to the
(grand-)*parent's locals, this could simply copy the object into a local
variable at definition time?

Example:

>>> def f1(x):
...  y = x + 2
...  def f2(x):
...    global y
...    print x+y
...  y = 0
...  return f2
...
>>> f = f1(3)
>>> del f1
>>> f(5) # the behavior below would print 5, and force f1's entire locals()
to stay alive.
10


Guido van Rossum <guido at cnri.reston.va.us> wrote in message
news:5l67149gwc.fsf at eric.cnri.reston.va.us...
> skaller <skaller at maxtal.com.au> writes:
>
> > The following file and viperi run demonstrate the current handling
> > of the globals statement in viper.  What happens is: global causes
> > a variable to refer to the containing context, often a module,
> > but in this demo, there is also a nested function, global then refers
> > to the locals() of the containing function, unless the variable
> > is _also_ declared 'global' in that function.
>
> Hm, this looks elegant, although I also see the elegance of
> disallowing assignments into the parent function altogether (i.e., you
> can *read* your parent's locals, but you can't *write* them).  So I'm
> undecided.
>
> --Guido van Rossum (home page: http://www.python.org/~guido/)






More information about the Python-list mailing list