[Tutor] subclassing across multiple modules
Lloyd Kvam
python at venix.com
Fri Mar 18 02:36:49 CET 2005
You want a method in a base class to parse input and create instances of
certain derived classes. 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):
node1 = Node1
node2 = Node2
...
class Nodes(Node,Toolkit.Node1):
...
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. 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.
--
Lloyd Kvam
Venix Corp
More information about the Tutor
mailing list