Changing self: if self is a tree how to set to a different self

Bart Kastermans bkasterm at gmail.com
Thu Jul 10 15:19:38 CEST 2008


I am playing with some trees.  In one of the procedures I wrote
for this I am trying to change self to a different tree.  A tree
here has four members (val/type/left/right).  I found that self = SS
does not work; I have to write self.val = SS.val and the same for
the other members (as shown below).  Is there a better way to do this?

In the below self is part of a parse tree, F is the parse tree of a
function f with argument x.  If a node in the parse tree is labelled
f, we should replace it by the parse tree for the function f, F, with
the remainder of the tree substituted for the input variable for the
function f, here x.

    def elimF (self):
        if self.val == "f":
            SS = F.copy ()
            SS.subst ('x', self.left)
            self.val = SS.val        # from here: set self to be SS
            self.type = SS.type
            self.left = SS.left
            self.right = SS.right    # completed: set self to be SS

        if self.left != None:        # iterate onward, inf recursion if f
                                     # appears.  Would need a check in
                                     # real version
            self.left.elimF ()
        if self.right != None:
            self.right.elimF ()

Best,
Bart



More information about the Python-list mailing list