Modifying a variable in a non-global outer scope?

Edward C. Jones edcjones at comcast.net
Fri May 19 15:14:25 EDT 2006


#! /usr/bin/env python
"""
When I run the following program I get the error message:

UnboundLocalError: local variable 'x' referenced before assignment

Can "inner" change the value of a variable defined in "outer"? Where
is this explained in the docs?
"""
def outer():
     def inner():
         x = x + 1

     x = 3
     inner()
     print x

outer()



More information about the Python-list mailing list