[Tutor] Accessing local variables from nested functions.

Krzysztof Wrzalik krzys_kyku at tlen.pl
Sun Feb 13 11:41:27 CET 2005


Hello all,
what's the Python way of accessing local variables in nesting functions? For 
example if I have:

def p():
  var1 = 3
  def q():
     print 'var1 in p is', var1
  q()

then there's no problem in running such function, but what if I'd like to 
modify var1 so that the change were vissible in p()?

I can't label var1 as global in q, because it then creates a new var1 in the 
module scope, leaving the nested one untouched.

Thanks in advance


More information about the Tutor mailing list