Search code examples
pythonscoping

Python scoping problem


I have a trivial example:


def func1():
    local_var = None

    def func(args):
        print args,
        print "local_var:", local_var

        local_var = "local"

    func("first")
    func("second")

func1()

I expect the output to be:

first local_var: None
second local_var: local

However, my actual output is:

first local_var:
Traceback (most recent call last):
  File "test.py", line 13, in 
    func1()
  File "test.py", line 10, in func1
    func("first")
  File "test.py", line 6, in func
    print "local_var:", local_var
UnboundLocalError: local variable 'local_var' referenced before assignment

My understanding of python scoping rules dictate that this should work as expected. I have other code where this works as expected, but reducing one non-working code fragment to it's trivial case above also doesn't work. So I'm stumped.


Solution

  • The assignment to local_var in func makes it local to func -- so the print statement references that "very very local" variable before it's ever assigned to, as the exception says. As jtb says, in Python 3 you can solve this with nonlocal, but it's clear from your code, using print statements, that you're working in Python 2. The traditional solution in Python 2 is to make sure that the assignment is not to a barename and thus does not make the variable more local than you wish, e.g.:

    def func1():
        local_var = [None]
    
        def func(args):
            print args,
            print "local_var:", local_var[0]
    
            local_var[0] = "local"
    
        func("first")
        func("second")
    
    func1()
    

    the assignment to the indexing is not to a barename and therefore doesn't affect locality, and since Python 2.2 it's perfectly acceptable for nested inner functions to refer to variables that are locals in outer containing functions, which is all this version does (assigning to barenames is a different issue than referring to variables).