assigning in nested functions

Fredrik Lundh fredrik at
Sun Oct 9 14:08:10 CEST 2005

"jena" wrote:

> I have code
> def test():
>   def x():
>     print a
>     a=2 # ***
>   a=1
>   x()
>   print a
> test()
> This code fails (on statement print a in def x), if I omit line marked
> ***, it works (it prints 1\n1\n). It look like when I assign variable in
> nested function, I cannot access variable in container function.
> I need to assign variable in container function, is any way to do this?


see "Rebinding names in enclosing scopes" in the design document
for a discussion (and a workaround):


More information about the Python-list mailing list