[Tutor] data structure question

Tiger12506 keridee at jayco.net
Fri Jan 18 22:47:09 CET 2008


> <snip>
> class Task(object):
> def __init__(self, cargo, children=[]):
> self.cargo = cargo
> self.children = children
>
> def __str__(self):
> s = '\t'.join(self.cargo)
> return s
>
> def add_child(self,child):
> self.children = self.children + [child]

This is an excellent start.
self.children = self.children +[child]
can be
self.children.append(child)

Building on your concept and Kent's suggestions, I wish to list some things 
that will
help me to organize this...

* Each task is a container for other tasks (children)  (Essentially a *list* 
of other tasks)
* Each task has 'cargo' which is a string
* Each task can print it's direct contents, or a full recursive print is 
available

This helps, I think.

So the answer is -> when you add a child to self.children, make it an 
instance of Task
Oh ~ and ~

class Task(object):
   ...
    def recursive_print(self, level=0):
        print "\t"*level + self.cargo
        for x in self.children:
          recursive_print(x,level+1)

should take care of the recursive printing with level control 



More information about the Tutor mailing list