Recursive structures

Steven Bethard steven.bethard at gmail.com
Mon Dec 20 13:46:59 EST 2004


Scott David Daniels wrote:
>     if mytype not in AVOIDITER:
>         try:
>             for item in obj:
>                 walks(item, seen)
>         except TypeError:
>             pass
>     try:
>         for key, value in obj.items():
>             walks(key, seen)  # Key might be object w/ hash method
>             walks(value, seen)
>     except AttributeError:
>         pass

You might also write this section something like:

if mytype not in AVOIDITER:
     try:
         itr = iter(obj)
     except TypeError:
         pass
     else:
         for item in itr:
             walks(item, seen)
             try:
                 walks(obj[item], seen)
             except TypeError:
                 pass

This should allow you to support any mapping type that supports iter() 
over the keys and the __getitem__ protocol.

Steve



More information about the Python-list mailing list