I have function defined like this:
def foo(karg=None):
def bar():
print(karg)
if karg is None:
karg = None
return bar
foo()()
Running above code results in following error:
Traceback (most recent call last):
File "/home/xps/a.py", line 10, in <module>
foo()()
File "/home/xps/a.py", line 3, in bar
print(karg)
UnboundLocalError: local variable 'karg' referenced before assignment
If I delete if branch in above function, code runs just fine:
def foo(karg=None):
def bar():
print(karg)
return bar
foo()()
Shouldn't karg
be visible all throughout bar()
function?