Python lets you access a list, but not an int from an nested inner function?

def func1():
    nums = [1,3]
    j = 1
    def func2(k):
        nums.append(k)
        print(nums)
        j = j + k
        print(j)
    func2(5)
func1()

I understand that all arguments passed to outer function, func1() , will be available to it's inner function func2(). When it comes to variables defined inside the outer function, like nums and j above, I can only access/modify the list and not the int. I get the output below:

    [1, 3, 5]
Traceback (most recent call last):
  File "<input>", line 10, in <module>
  File "<input>", line 9, in func1
  File "<input>", line 7, in func2
UnboundLocalError: local variable 'j' referenced before assignment

We can see the argument get appended to the nums in the output, but it throws an error when it gets to j = j + k. The same is true for sets, and dictionaries, they can be accessed from an inner function. How come I cannot access the int, but I can access the list, even though they are defined in the same scope?


The problem is that when you are assigning to j inside func2, you are creating a new local variable inside that function. Python then tries to reference that local variable (j) on the right-hand side of the assignment, and can't find any value assigned to it yet.

A possible solution is to use the nonlocal keyword on j like this:

def func1():
    nums = [1,3]
    j = 1
    def func2(k):
        nonlocal j
        nums.append(k)
        print(nums)
        j = j + k
        print(j)
    func2(5)
func1()

Which produces

[1, 3, 5]
6

This post is related an might give you some more clarity: Python 3: UnboundLocalError: local variable referenced before assignment

Also some more information from the Python docs: https://docs.python.org/3/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value