I have the following Python code:
def find_words(letters):
results = set()
def extend_prefix(w, letters):
if w in WORDS: results.add(w)
if w not in PREFIXES: return
for L in letters:
result = extend_prefix(w + L, letters.replace(L, "", 1))
results = results | result
extend_prefix('', letters)
return results
print find_words("ABCDEFGH")
When I run it, I get the following error:
Traceback (most recent call last):
File "ExtendPrefix.py", line 44, in <module>
print find_words("ABCDEFGH")
File "ExtendPrefix.py", line 41, in find_words
extend_prefix('', letters)
File "ExtendPrefix.py", line 38, in extend_prefix
result = extend_prefix(w + L, letters.replace(L, "", 1))
File "ExtendPrefix.py", line 38, in extend_prefix
result = extend_prefix(w + L, letters.replace(L, "", 1))
File "ExtendPrefix.py", line 35, in extend_prefix
if w in WORDS: results.add(w)
UnboundLocalError: local variable 'results' referenced before assignment
It apparently can’t find results in a recursive call to extend_prefix. Why is this and how can I fix it?
Because you’re assigning results inside of a nested function, Python assumes you’re using a locally scoped variable and throws up at line 35 even though it’s a valid name in a higher scope. If you were only reading the variable and not writing to it, it will oftentimes work on the higher namespace object. But as soon as an assignment operator appears, you jump to local namespace.
From Python scopes/namespaces:
To get around this, the easiest is to pass the variable you want use into the function header:
Also the way you wrote the function, you weren’t returning a set, so the
results = results | resultwould have blown up with results being None Type.