Consider this example:
def func1():
val = 1
res = [1]
def fun2():
print(res)
print(val)
val = 2
fun2()
print(val)
func1()
It raises the following exception:
UnboundLocalError: local variable 'val' referenced before assignment
List res
can be accessed by fun2
, but val
cannot. I know list
is mutable and int
is not, but is there a way to make val
accessible by fun2
as well? In a class, I could easily achieve that with self.val
, but is there a way to do it inside a function?