global in nested functions (viper)

skaller skaller at maxtal.com.au
Tue Sep 21 12:17:00 EDT 1999


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.

This behavious is not strictly python compatible, but will only
cause problems in nested functions.

Any comments?

[BTW: you can see a nested function q() being returned here,
it is bound to the activation record of h() which created it.]

[root at ruby] ~/viper>cat t_globals.py
print "test globals and nested functions"

x = 1
def f(y):
  global x,y
  z = 1
  y = 2
  x = 3

f(3)
print x,y

z = 900
def h():
  global z
  x = 100
  def q():
    global z
    global x
    x = x + 1
    z = z + 1
    return x
  q()
  return q

qq = h()
print qq(),z
print qq(),z

[root at ruby] ~/viper>./viperi t_globals.py
INSTALLING MODULE exceptions
INSTALLING MODULE py_types
EXECUTING PY_TYPES.PY
install_modules: cannot find file for strop
INSTALLING MODULE types
Viperi 2.0.1
Copyright Maxtal P/L, John Skaller, Australia, 1999
test globals and nested functions
3 2
102 902
103 903
DONE






More information about the Python-list mailing list