I'm having trouble figuring out an error in Python that I haven't gotten in other languages that support functional programming. I have the following code:
def call_once(func):
called = False
def aux(*args, **kwargs):
if called:
raise Exception("This function was already called.")
else:
called = True
return func(*args, **kwargs)
return aux
@call_once
def add(x, y):
return x + y
However, if I have:
add(1, 2)
I get:
UnboundLocalError: local variable 'called' referenced before assignment
What's going on here?