OO approach to decision sequence?

John Machin sjmachin at lexicon.net
Sat Jun 18 03:52:57 EDT 2005


Jordan Rastrick wrote:
> I've coded some simple recursive tree data structures using OO before
> (unfortunately not in Python though). It's not nessecarily an
> ill-suited approach to the task, although it depends on the specific
> details of what you're doing. What's the the piece of code from which
> your if...elif fragment is taken actually supposed to do?
> 
> Without knowing more about your problem, I think the most obvious OO
> approach would be to write a seperate (simple) class for each of
> node_type_1, node_type_2, etc. Make sure each one provides the same
> interface, i.e. defines the same set of methods. Then put the different
> decision branches as implementations of a certain method in each class.
> 
> class Node_Type_1(object):
>     def recurse(self):
>          # do stuff here
> 

etc etc

and perhaps if you found by the time you got to Node_Type_2 that its 
opensesame() method was identical to the opensesame() method for 
Node_Type_1, you might decide that having separate simple classes could 
be improved on by having a Node class, which you would subclass for each 
Node_Type_n ...



More information about the Python-list mailing list