Variables in nested functions

Bryan Olson fakeaddress at nowhere.org
Wed Aug 30 04:43:16 EDT 2006


Ben Cartwright wrote:

> The typical kludge is to wrap the variable in the outer function inside
> a mutable object, then pass it into the inner using a default argument:
> 
> def outer():
>     a = "outer"
>     def inner(wrapa=[a]):
>         print wrapa[0]
>         wrapa[0] = "inner"
>     return inner

As of Python 2.2, scopes nest, as per PEP 227.
We still need a mutable, but not a default argument:

def outer():
     a = ["outer"]
     def inner():
         print a[0]
         a[0] = "inner"
     return inner


-- 
--Bryan



More information about the Python-list mailing list