[Tutor] Accessing local variables from nested functions.
Alan Gauld
alan.gauld at freenet.co.uk
Sun Feb 13 17:13:02 CET 2005
> what's the Python way of accessing local variables
> in nesting functions?
THats usually bad practice, and since it is a nested function
why not just return a value?
def p():
var1 = 3
def q():
print 'var1 in p is', var1
return 42
var1 = q()
After all var1 only exists within p() so you might as well
assign it there. If you need to return more than one value
recall that Python can return a tuple:
def f():
v = 1
u = 2
def g()
w = 42
x = 24
return w,x
u,v = g()
return u
Does that help?
Alan G.
PS In other words I don't know the real answer to your
question! Other than maybe messing with locals dictionaries
etc that are really horrible and messy :-)
More information about the Tutor
mailing list