[Tutor] help with refactoring needed -- which approach is morePythonic?

Brian van den Broek bvande at po-box.mcgill.ca
Fri Feb 11 17:15:25 CET 2005


Alan Gauld said unto the world upon 2005-02-10 02:58:
> Pseudo code:
>        class Body:
>           def __init__(self,content):
>             self.contents = contents
>             self.nodes = []
> 
>           def parse(self):
>              for line in self.contents:
>                  if line == NodeStartTag:
>                     node = Node()
>                  if line == NodeEndTag:
>                     self.nodes.append(node)
>                  node.append(line)
> 
>        class Node:
>          def __init__(self,lines=[]):
>               self.lines = lines
>          def append(self,item):
>               self.lines.append(item)
>          def parse(self):
>               # your parsing method here.

Hi all,

YAQ (Yet Another Question):

Following the general pattern, I end up with a Body object which has 
an attribute .nodes that consists of a list of Node objects.

So, something like:

My Example Body
    Node List
       Node the first
       Node the second

Is there any way to make methods of the Node class access attributes 
of `parents' of instances? I would like a Node instance such as Node 
the first above to be aware just what it is a node of and what its 
siblings are.

Does this make sense?

Best to all,

Brian vdB

PS Thanks for the reply to my venting question, Kent.



More information about the Tutor mailing list