Variables in nested functions

zero.maximum at gmail.com zero.maximum at gmail.com
Tue Aug 29 20:55:13 EDT 2006


Is it possible to change the value of a variable in the outer function
if you are in a nested inner function?

For example:

def outer():
     a = "outer"
     def inner():
          print a
          a = "inner"
               # I'm trying to change the outer 'a' here,
               # but this statement causes Python to
               # produce an UnboundLocalError.
     return inner

What I'm trying to do here is to get the following output from these
lines of code?
# Code:
func = outer()
func()
func()
func()

# Output:
outer
inner
inner




More information about the Python-list mailing list