[Tutor] Re: subclassing across multiple modules
Brian van den Broek
bvande at po-box.mcgill.ca
Fri Mar 18 05:40:18 CET 2005
Hi all,
Thanks for the reply, Lloyd.
Lloyd Kvam said unto the world upon 2005-03-17 20:36:
> You want a method in a base class to parse input and create instances of
> certain derived classes.
Not quite. One class (Tree) parses a file and creates instances of
Node1 and Node2 (derived from Node). None of the Node classes inherit
Tree. But I don't think that difference matters for what you
suggested. :-)
> Your sample code looks like:
> if some_condition_on_chunk_contents:
> node = Node1(chunk_contents)
> else:
> node = Node2(chunk_contents)
>
> I'd suggest changing the method to use a variable to determine the
> class. Following your pattern of coding:
> #Toolkit.py
> class Tree:
> node1 = Node1
> node2 = Node2
> ...
> class Node1(Node):
> ...
>
> #Application.py
> class Tree(Toolkit.Tree):
<SNIP -- same as in Tree immediately above>
> if some_condition_on_chunk_contents:
> node = self.node1(chunk_contents)
> else:
> node = self.node2(chunk_contents)
>
> This requires that the class constructors be similar enough so that they
> take the same parameters.
Happily, the interfaces are identical. :-) (I need different Node
classes as the chunk_contents are in different formats, the tasks are
the same, modulo differences due to format.)
> It is also a manual effort to keep the node
> assignments in Tree in sync with the Node classes that you write. You
> also face a similar issue keeping Application.py and Toolkit.py in sync.
Hmm. I think I do see some appeal to the route you suggest, but I'm
feeling unhappy about the need to keep multiple things in sync.
I think I will play with this and other alternatives and see what
feels right. Thanks so much for the input :-)
Best to all,
Brian vdB
More information about the Tutor
mailing list