I was writing a decorator that is initialized with an argument, and I tried setting that argument to None by default and then check if that argument is None and if so do argument = some_default_value. I've boiled down the issue I ran into:
This code works (it prints None):
def outer(arg=None):
def inner():
print(arg)
inner()
outer()
and this code works (it prints 22):
def outer(arg=None):
def inner():
arg = 22
print(arg)
inner()
outer()
This code doesn't:
def outer(arg=None):
def inner():
if arg is None:
arg = 22
print(arg)
inner()
outer()
I get the following error:
Traceback (most recent call last):
File "test.py", line 8, in <module>
outer()
File "test.py", line 6, in outer
inner()
File "test.py", line 3, in inner
if arg is None:
UnboundLocalError: local variable 'arg' referenced before assignment
Why does using the variable arg in an if statement make Python decide that it must be defined in that scope and not in an outer scope?