[[...]]

Alex alex at somewhere.round.here
Sat Mar 18 11:49:00 EST 2000


> It would be nice to have some more bulletproof way than the only other
> approach I can think of:

See Tim Peters' plumbo.py for something more complete along these lines:

from types import ListType

def IsRecursiveList(x):
    element_ids = {}
    element_ids[id (x)] = None
    for element in x:
        if type (element) == ListType:
            element_id = id (element)
            if element_ids.has_key (element_id):
                return 1
            else:
                element_ids[element_id] = None
    else:
        return 0

if __name__ == '__main__':
    a = []
    a.append (a)
    assert IsRecursiveList (a)
    assert not IsRecursiveList ([])            

Alex.



More information about the Python-list mailing list