I\'ve got this piece of code:
#!/usr/bin/env python
def get_match():
cache=[]
def match(v):
if cache:
return cache
cache=[v]
return ca
Since Python sees cache=[v] - assignment to cache, it treats it as local variable. So the error is pretty reasonable - no local variable cache was defined prior to its usage in if statement.
You probably want to write it as:
def get_match():
cache=[]
def match(v):
if cache:
return cache
cache.append(v)
return cache
return match
m = get_match()
m(1)
Highly recommended readings: Execution Model - Naming and binding and PEP 227 - Statically Nested Scopes