[Tutor] Restricting the type of passed-in objects

VanL van@lindbergs.org
Sun, 13 May 2001 16:56:34 -0600


Roeland Rengelink wrote:

So, how do you go about designing your Tree interface?

>
> It depends on what you're going to use the tree for.
>
> I can think of several different answers.
>
> o The tree is going to be the underlying data structure for a
>   sequence.  For example, an AVL-tree to implement a sorted list
>
> o The tree is going to be the underlying data structure for a
>   mapping. To make a UserDict with sorted keys for instance.
>
> In these cases the interface to your tree is pretty much determined
> by the interface to your sequence or mapping respectively.
>
> Another possibility is that you want to be able to inherit
> a particular class from a tree. For example:
>
> class FileSystem(Tree):
>     pass
>
> In this case the fact that the object is a tree seems central to the use
> of the object (while in the previous cases, it was an implementation
> detail).
>

Here is how I came to this project:

I was thinking about writing a faq generator and maintainer for my work.  While
thinking about the problem, it occurred to me that a faq could be easily
represented by a tree of faqEntry objects.  Then I could also build into each
entry the knowledge of how to render itself in various formats (html, text,
whatever).  Then I could just call faq.render() on the root of the tree and it
would recursively call itself to render the whole thing, including all the numbers

and hyperlinks (because the relationships would be defined by each entry's
position in the tree).

After I started to work on that problem (and being a data structures junkie -- I
just think they are cool) it seemed to me that it would be much better to write a
general tree class and inherit from it.  Then, I started to think how I could make

that tree class be as general as possible, so that I could use it in the future to

represent different types of trees.

And thus we are here.  If my faq-o-matic works like I hope, I think it would be a
pretty cool little hack -- It would be the first thing I would consider neat
enough to submit to Useless Python.

Van