Skip to content Skip to sidebar Skip to footer

Unboundlocalerror In Recursive Call Of Nested Function

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 PREFIXE

Solution 1:

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:

A special quirk of Python is that – if no global statement is in effect – assignments to names always go into the innermost scope. Assignments do not copy data — they just bind names to objects.

To get around this, the easiest is to pass the variable you want use into the function header:

def extend_prefix(w, letters, results):
        if w in WORDS: results.add(w)
        if w not in PREFIXES: return
        for L in letters:
            extend_prefix(w + L, letters.replace(L, "", 1), results)

Also the way you wrote the function, you weren't returning a set, so the results = results | result would have blown up with results being None Type.

Solution 2:

Alternatively, you wouldn't need a nested function if you used a default value of None for results and then initialized results as an empty set. It simplifies your code a bit and fixes your problem. See below...

deffind_words(letters, pre = '', results = None):
    if results isNone: results = set()
    if pre in WORDS: results.add(pre)
    if pre in PREFIXES:
        for L in letters:
            find_words(letters.replace(L, '', 1), pre+L, results)
    return results

It's good to see another Udacity student on StackOverflow!

Post a Comment for "Unboundlocalerror In Recursive Call Of Nested Function"