bizarre Recursive/class interaction

Bob Roberts bobnotbob at byu.edu
Tue Mar 11 01:28:10 EST 2003


The following code gives me an assertion error from the line
assert( not ret.data)
which means that some how ret.data is getting set to something besides
an empty list.  I don't see how this can happen since nothing is
passed into the construction of ret and ret.data defaults to an empty
list.


import UserList

def JunkRecursive(line):
    print line
    gotH = 0
    ret = MyClass()
    assert( not ret.data)
    i = 0
    while i < len(line):
        char = line[i]
        print char
        if char == "a":
            ret.append(1)
        elif char == "h":
            if not gotH:
                gotH = 1
            else:
                subList = JunkRecursive(line[i:])
                ret.append(subList)
        i += 1
    return ret

class MyClass(UserList.UserList):
    pass
    def __init__(self, data = []):
        UserList.UserList.__init__(self)
        self.data = data
        if self.data:
            print self.data


line = "haaah"
JunkRecursive(line)




This gives the output:
haaah
h
a
a
a
h
h
[1, 1, 1]
Traceback (most recent call last):
  File "junk.py", line 33, in ?
    JunkRecursive(line)
  File "junk.py", line 18, in JunkRecursive
    subList = JunkRecursive(line[i:])
  File "junk.py", line 7, in JunkRecursive
    assert( not ret.data)
AssertionError




More information about the Python-list mailing list