0

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?

Brandon
  • 1,336
  • 3
  • 10
  • 38
  • http://stackoverflow.com/questions/141642/what-limitations-have-closures-in-python-compared-to-language-x-closures probably would've been a better dupe candidate than the one I picked. – user2357112 Jun 16 '14 at 00:06
  • Try running it this way: http://stackoverflow.com/questions/4103773/efficient-way-of-having-a-function-only-execute-once-in-a-loop – karthikr Jun 16 '14 at 00:07

0 Answers0