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?