Please do not ask how I get myself into this situation.
Lets say I have a class called
this class has the following attributes at runtime:
ccollection.a.b.x = 1 ccollection.a.b.y = 3 ccollection.a.b.z = 4 ... ccollection.a.c = 3 ccollection.b = 3
this class will be setup dynamically as described above. so there is no way to know the attributes in the class before hand.
Now I would like to print all the attributes in this class, for example:
ccollection.a.b should print
ccollection.a.b.x = 1 ccollection.a.b.y = 3 ccollection.a.b.z = 4
ccollection.a should print
ccollection.a.b.x = 1 ccollection.a.b.y = 3 ccollection.a.b.z = 4 ccollection.a.c = 3
I think you get the idea. Each print should starts printing all the elements at the same level and below. I am looking for a way to recursively traverse all the attributes (which is a tree-like data structure)
This situation really calls for refactoring. You are using an object that is not designed as a container. Instead, use a container such as a dict or a class that inherits from dict.
If you must use the current setup, I agree with Blckknght that the most promising approach appears to use dir.
class CCollection(object): def get_children_strings(self): list_of_strings =  for attr_name in dir(self): if attr_name not in dir(CCollection()): attr = getattr(self, attr_name) if hasattr(attr, 'get_children_strings'): list_of_strings.extend(["." + attr_name + child_string for child_string in attr.get_children_strings()]) else: list_of_strings.append("." + attr_name + " = " + str(attr)) return list_of_strings def print_tree(self, prefix): print [prefix + s for s in self.get_children_strings()]
Then you can
m = CCollection() m.a = CCollection() m.a.b = CCollection() m.a.b.x = 1 m.a.b.y = 2 m.a.c = 3 m.d = 4 m.print_tree("m") m.a.print_tree("m.a") m.a.b.print_tree("m.a.b")
and get the outputs:
>>> m.print_tree("m") ['m.a.b.x = 1', 'm.a.b.y = 2', 'm.a.c = 3', 'm.d = 4'] >>> m.a.print_tree("m.a") ['m.a.b.x = 1', 'm.a.b.y = 2', 'm.a.c = 3'] >>> m.a.b.print_tree("m.a.b") ['m.a.b.x = 1', 'm.a.b.y = 2']
To take this further, you probably would want to use a class with tree-traversal functions. You could automatically generate the info currently being passed in via the
prefix argument to the
print_tree function, if you had a function to get the parent node, a guarantee of no loops, and a class variable holding the node's name.
External links referenced by this document: