Recusive function inside a function can't find itself.

Glyn Webster glyn at ninz.not.org.nz
Fri Oct 22 23:14:35 EDT 1999


I've defined a function inside a function. I pass all
the variables it needs into it through its argument
list, which usually works. But what happens this time
is that I get a "NameError: fill_code_list" where the
function first calls itself recusively.

Am I doing something wrong?
(Other than pretending Python is Scheme?)

def display_codes(node):
    """ Pretty-prints the weight and codes for all
        items in the Huffman tree hanging from `node'.
    """
    def fill_code_list(list, node, code):
        if len(node.items) == 1:
            item = node.items[0]
            list.append((item, node.weight, code))
        else:
            fill_code_list(list, node.lower, code + "0")  #SPLAT
            fill_code_list(list, node.higher, code + "1")
    list = []
    fill_code_list(list, node, "")
    list.sort(lambda a,b: -cmp(a[1], b[1])) #By descending weight.
    for (item, weight, code) in list:
        print "  %-6s %4i  %s" % (`item`, weight, code)

--
(Remove the negation from my address to reply by email.)
"ThE oRigiN and SigNificaNce of thIs pRacTicE iS oBscuRe."




More information about the Python-list mailing list